How to use SQL - DATEDIFF function - sql

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

Related

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

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())

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)

sql server dateadd() using column name year and not keyword 'year'

I am using DATEADD(year, -3, GETDATE()) to pull the last thee years worth of data on a rolling period.
I also have a column called Year, the view I am creating is using the MyTable.Year and not the sql keyword 'year'.
e.g DATEADD(MyTable.year, -3, GETDATE())
It resolves it every time in the view which is really annoying. I'm a bit rusty, been out of this for about 4 years.
How do I make sure it uses the keyword 'year', I find it strange it is doing this. Any explanation on this would also be helpful. SQL Server 2016
Thanks guys.
EDIT: I have edited my misplaced schema notation and identified the table name, sorry for confusion
I am not sure if I understand correctly, but you want to enforce the keyword, so you want to subtract 3 years from the current date, correct? This should work:
SELECT DATEADD(yy, -3, GETDATE())
or
SELECT DATEADD(yyyy, -3, GETDATE())
(unless you have columns named yy and yyyy ;-))
Otherwise forgive my misunderstanding...
It seems your editor has problems with the keyword YEAR and replaces it with a value from a column that is also called year
This can be solved by using a synonym for the keyword year in the DateAdd function.
So instead of
dateadd(year, -3, getdate())
use
dateadd(yy, -3, getdate())
You could put your table in a subquery and alias [Year] column.
Something like
SELECT ... FROM (SELECT [Year] AS "MyYear" FROM ...) X
(or use CTE)
Then Year would only mean the keyword.

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 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())