Finding the DATEDIFF between two columns and creating its own column [duplicate] - mysql

This question already has answers here:
How to get the number of days of difference between two dates on MySQL?
(8 answers)
Closed 4 years ago.
Trying to find the DATEDIFF between two dates and display it as it's own column.
Here are my current columns:
currentcolumns
And here is what I need:
needed
Here is the code I have been running with error:
SELECT orderNumber, DATEDIFF(day,orderDate,shippedDate) AS day FROM datenumtest;
The error I get says: #1582 - Incorrect parameter count in the call to native function 'DATEDIFF'
I've looked at a ton of sites now and can't seem to see what the issue is. Ideas?

MySQL's DATEDIFF function just takes two parameters:
SELECT orderNumber, DATEDIFF(shippedDate, orderDate) AS day
FROM datenumtest;
Note the order of the date parameters used, which would return some positive number of days assuming that the shipping date be greater than the order date.

DATEDIFF function in MySQL only supports two input parameters, whereas SQL Server variant of the function supports to derive days, month etc by passing in the additional parameter.

Related

Trying to calculate the number of working days from a given date and to exclude weekends in MYSQL

I'm trying to select a column that would reference a date-time value and allow me to find the date given an addition or subtraction of weekdays from the referenced date. I know I can calculate the number of days between with DateDiff but that hasn't seemed as useful in trying to determine a landing date X number of days from a given reference. Is there a way of leveraging DATE_ADD or another function for this?

SQL query with date filter but use system year

I´m creating a static reports for every month for every year from our ERP database, contract table. I've managed to get everything right but now I have to alter the monthly queries every year. Currently I use this to separate the months:
WHERE [Ending Date] >= '04.01.2021' and [Starting Date] <= '04.30.2021'
As said the output is right but what I want is the query to use system year, so only dates are input in the query and therefore when the year changes, it would change in the query automatically.
The question isn't too clear, but what I vaguely understood is you want to get the year dynamically (not sure if you want just the year or the whole date) but you can make use of YEAR() function (something like YEAR(CURDATE()) could get you the year of the current date). You can also take a look here for more useful functions.
I couldn't form a query or tell you what functions to exactly use because I doubt I'm fully understanding. Some functions you may make use of are PERIOD_ADD() and TIMESTAMPADD().

find if data existed 6 months ago ssrs

In SSRS report builder I am trying to write an expression for calculated fields in my dataset to see if this data exists now, existed 6 months ago, and existed a year ago. (So 3 different calculated fields).
I don't want to use different datasets as the information all needs to go into a table with a percentage change column.
I have the from and to dates of the data and have been trying to write something along the lines of
=IIF(Fields!From.Value<-6months AND Fields!To.Value>-6months, true, false)
I'm obviously missing something in my expression but my knowledge in this is limited. Any ideas on how to get this to do what I want it to?
my data basically is this (but being pulled from various tables in our database)
person ID - condition 1 - from date - to date
person ID - condition 2 - from date - to date
person ID - condition 1 - from date - to date
person ID - condition 3 - from date - to date
etc.
I've been asked to make a table with the conditions as the rows, the columns are "exists now", "existed 6 months ago" and "existed 12 months ago" and a count of the person IDs as the data
the from and to fields are date fields
The visibility thing can be a little confusing sometimes, as with everything in SSRS it can be finicky.
Since you're new at this I would recommend something easy like trying to hide your first row. You'll probably want to do this by right clicking the table and going to properties. Under visibility, you can add an expression. Try hiding a row by the unique ID or something like that first...
=IIF(Fields!UID.Value=1234, TRUE, FALSE)
The reason I say to go through this is to make sure you're able to properly hide a row. If you are using the visibility in groups instead, sometimes it doesn't work expected. Anyway, once you get that working, you'll want to do something like this.
=IIF( (DateDiff("m", CDate(Fields!FromDate.Value), Now()) < 5 AND DateDiff("m", CDate(Fields!ToDate.Value), Now()) > 5), FALSE, TRUE)
You'll be using the DateDiff function to calculate the difference between the date and NOW() (which returns current date). The "m" specifies you want to know how many months between dates. CDate will cast your field into a DateTime object.

Using MySQL to return row between to strings with date, but without year

I am currently working on a project in which I want to store commemorative days (like January 8th's World Leprosy Day) in a database. At this moment they're stored in a table which contains:
- an ID
- the date as varchar (stored European style, e.d. "8-01" for January 8th)
- length of the commemorative day (as some span multiple days)
- and the name
The reason I am storing the date as varchar is because the year is irrelevant, and I'm a bit reluctant to just store a year (e.g. 2013) in the database and truncate it.
But here's the problem: I can't seem to find a way to construct a query that will get the rows between dates. I think it's because the way the dates are stored in the database.
I already tried (given day = "8-01")
SELECT * FROM comdays WHERE date(day) BETWEEN date("1-01") AND date("20-01")
But to no avail.
Is there a way to get this thing going with strings? Or do I have to change the date column into a MySQL DATE format?
Thanks in advance!
If you really want to keep non standard date field in MYSQL you will need to use the following format 0108-> mmdd this format allows calculations.
It might also be worth reading the following answers to similar question Save day and month in database

fetch only date from date datatype in sql server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to return the date part only from a SQL Server datetime datatype
I need to fetch only date from sql server eventhough i have used date datatype i am getting time also. but in database n_strat_date column has only date value not the time value.
My table:
My Output:
Regardless of how you store the dates in SQL, if you load this data in C# DateTime values, they will always have a time part (which is 12:00:00 AM, unless otherwise specified).
Your problem here has nothing to do with SQL; it's a formatting issue in your application.
Concretely, if you had the DateTime value in a variable called startDate, instead of printing out to the form startDate or startDate.ToString(), use an explicit format, like this: startDate.ToString("M/d/yyyy").
Here's an online test to see how it works.
try it, it will work
select CONVERT(date,column_name) from table_name