How can I get a timestamp or a data using datediff function? - sql

I am reading some sql code from somebody where DATEDIFF function should return a timestamp. Here is the code:
DATEDIFF(wk, 6, GETDATE())
I do not see any sql syntax like this anywhere. Can anyone explain how this code should return date or timestamp?

You use DATEDIFF() function to get the difference between two dates, and it returns integer value. Looking at what you are trying to do, it seems you want to add 6 weeks to the current date. In such cases you should use DATEADD() function instead.
Below are some some examples:
SELECT DATEADD(ww, 6, GETDATE()),
DATEDIFF(ww,'6/1/2022', getdate())

Related

How to truncate a date to the first day of the month in Snowflake?

I am not able to find any good conversion code to get 2014-02-17 into 2014-02-01 without having to use concatenation and a ton of formatting.
I wonder if someone can help me find a good command to achieve this. Thanks!
Snowflake supports date_trunc() for datatypes DATE, TIME, and TIMESTAMP:
SELECT DATE_TRUNC(month, CURRENT_DATE()) AS first_day_of_month;
This is another option:
SELECT TRUNCTIMESTAMPTOMONTH(CURRENT_DATE());
Sounds like you're working with strings. If that's the case and they'll always be in the format 'yyyy-MM-dd', you can just take the first 8 characters and add '01':
left(MyStringValue, 8) + '01'
If you're working with date fields, I like the trick of doing datediff to get the months from 0, then use dateadd with 0 to get back to the first of the month:
dateadd(month, datediff(month, 0, MyDateValue), 0)

How to use SQL - DATEDIFF function

need quick help here.
I'm coming up with a column to track how many days a event is back-logged.
So I'm using this syntax which is working
DATEDIFF(DAY, GETDATE(), ESI.EventStatusDate) AS BackloggedDays
However, the modification I want to use is, the event status date + 3 days. Any ideas how i would add that to this syntax. Thanks
I think you need DATEADD() :
DATEDIFF(DAY, GETDATE(), DATEADD(DAY, 3, ESI.EventStatusDate)) AS BackloggedDays

DateAdd function in Excel SQL query not working

I am using Excel to make an SQL query (connecting to an ODBC).
I have a problem with the function DateAdd(), which just doesn't work as expected.
What I need to do is to get the data from the last six months.
My code is something like
SELECT blablabla
FROM blablabla and then I have this:
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date> DateAdd(Month, -6, Now())
I've searched in the internet and this syntax is supposed to work, but I get this error message
Column "MONTH" cannot be found or is not specified for query. (13865)
As if it didn't have the parameters I think it has, which are "interval, number, date", but something else.
Any idea about this?
This is what you need:
DateAdd("m", -6, Now)
Or even DateAdd("m", -6, Date), if you want to get rid of the hours, coming from Now.
Thus, you have to declare that you want the "Months" to be substracted.
DateAdd MSDN
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date > ADD_MONTHS(curdate(), -6)

SQL automatic date range using DateSerial function

We've been using MS Access, with the following syntax for MTD Data that works for us:
Between DateSerial(Year(Date()),Month(Date()),1)
And DateSerial(Year(Date()),Month(Date())+1,0)
We need to transition the above logic to SQL/SSRS for automatic emailed reports, but I cannot get this DateSerial logic to work with SQL.
In the Filter field of the SQL query, I can successfully use BETWEEN '8/1/2014' AND '8/31/2014' for MTD data, but would like to have a DateSerial logic applied so that reports don't need to be created for every month, quarter, year, etc.
When trying to use the DateSerial function, we get the error "Invalid or missing Expression". I've seen a few topics on this that Parameters are required, but really believe that this is a simple syntax issue for the filter field, since actual dates work with the BETWEEN command.
There are several different ways to get this. Here is just one way.
Get todays date. In this case 8/27/2014
Declare #Today date = cast(getdate() as date)
Get the first of the month, 26 days in the past
Declare #StartDate date = dateadd(d, -1 * (day(#Today) - 1), #Today)
select #Today, #StartDate
You can use the function CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Or the function DATEFROMPARTS if you are using SQL Server 2012:
http://msdn.microsoft.com/en-us/library/hh213228.aspx
Or DATEADD:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0); -- first day of current month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) -- last day of current month
This last one I took from: https://stackoverflow.com/a/11746042/1274092
See mine at:
http://sqlfiddle.com/#!3/d41d8/38333
This has been resolved. The ODBC driver does not apparently play well with SSRS. The DateSerial command would not work within the query itself. The workaround was to add the filter to the Dataset. This syntax is what works, but again only in the Dataset filter: [expression] Between [first value box] =DateSerial(Year(Now()),1,1) [second value box] =DateSerial(Year(Now()),12,31)
This gives us the YTD reporting data that we require.

sql compare datetime today

I hope anyone can translate my abstract query.
I want to select * from TABLE where ( [MYDATETIMEROW] < (TODAY - 3 Days)).
Does I have to Convert, cast or use datepart or anything else?.. im confused.
Are there simple rules? I would'nt have problems to do that with linq but simple sql I learned just hardly.
Thank you and best regards.
In simple terms:
Select * from Table where MyDateTimeRow < dateadd(dd,-3,getdate())
But using getdate() will provide both a date and a time, experience says that this is unlikely to be exactly what you want - you might want to strip the time down and just consider the date portion
Select * From Table where MyDateTimeRow < dateadd(dd, datediff(dd, 0, getdate()) - 3, 0)
You want the DateAdd function to manipulate dates and the GetDate function to get the current date:
SELECT * FROM MyTable WHERE [MyDateTimeRow] < DateAdd(dd, -3, GetDate())