Microsoft SQL 2008 (using Visual Studio 2010 to build and test query).
Query function: This query is going to be run via batch file in Windows Task Scheduler. The goal is to be able to create a CSV file every month.
My query:
SELECT TYPE, DATEX, TIME, STREET, CROSS_ST, XCOORD, YCOORD
FROM INCIDENT
WHERE (TYPE = '644') OR
(TYPE = '459') OR
(TYPE = 'HS') OR
(TYPE = '484') OR
(TYPE = '487') OR
(TYPE = '488') OR
(TYPE = '10851') OR
(TYPE = '187') OR
(TYPE = '211') OR
(TYPE = '245') OR
(TYPE = '451')
ORDER BY DATEX
I am trying to sort the 'DATEX' column (which is in datetime format) between 'today's date' and '30 days ago'
I have tried all of these statements and none of them work:
(DATEX < DATEADD(month, - 1, GETDATE()))
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)),
(
CONVERT(varchar, DATEX, 110) AS DATE,
DATEX CONVERT(varchar, DATEADD(d,-30,GETDATE()), 23)
)
*ERROR INTERVAL is not recognized
AND DATEX BETWEEN DATEADD(mm,-1,GETDATE()) AND DATEADD(mm,1,GETDATE())
*error unrecognized syntax 1
=DateAdd("d", -7, Today()) *Today is not a recognized function
where date_col > DATEADD(day,-7,SYSDATETIME())
*unrecognized syntax near 'Where'
where DATEDIFF(day,date_col,SYSDATETIME()) < 7
*error near 'WHERE'
DATEX Dte < DATEADD(month, -2, GETDATE())
*an expression of non-Boolean type specified
AND (DATEX BETWEEN ({ fn CURDATE() }, 30) AND { fn CURDATE() })
*incorrect syntax near ','
What is the correct syntax for Microsoft SQL query to sort a table 'incident' between 'today's date' and '30 days ago'?
Please note that simply using Between 'YYYY-MM-DD' and 'YYYY-MM-DD'
is not helpful because I would need to manually change the dates to run the query. I want to automate it to create a CSV file every month.