DateAdd function in Excel SQL query not working - sql

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)

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

Enter Parameter Value when getting the last dat of the month

I want to get the last day of the month by using the following formula I found here at Stackoverflow:
SELECT DATEADD(month, ((YEAR(#test) - 1900) * 12) + MONTH(#test), -1)
I'm sure this formula should work but when I use it I get a pop-up where I'm asked to enter a parameter value for Month.
Does anyone know how I could solve this issue?
Thank you in advance.
This looks like SQL Server. If so, just use EOMONTH():
select EOMONTH(#test)
I realize that you might be using MS Access. If that is the case you can use date functions to get the value:
select DateAdd(d, -1,
DateAdd(m, 1,
DateSerial(Year(#test), Month(#test), 1))
)
)

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

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.

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.