nitayalevette
asked on
Sql Date Syntax
I am using the below sql syntax to pull data for the previous month, and its not pulling all data for the month. How can I adjust the below syntax?
DateOfOrder BETWEEN dateadd(month, -1, (dateadd ( day, -(DATEPART(day, getdate()))+1, getdate() )))
AND (dateadd(day,-(DATEPART(da y,getdate( )) ),getdate()) )
DateOfOrder BETWEEN dateadd(month, -1, (dateadd ( day, -(DATEPART(day, getdate()))+1, getdate() )))
AND (dateadd(day,-(DATEPART(da
DateOfOrder BETWEEN convert( datetime, -datediff(day,dateadd(mont h,-1,getda te()),day( getdate()) -1) )
AND convert( datetime, -datediff(day,getdate(),da y(getdate( ))) )
AND convert( datetime, -datediff(day,getdate(),da
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please try the following:
CAST(DateOfOrder as date) BETWEEN dateadd(month, -1, dateadd(day, 1 - DAY(getdate()), CAST(getdate() as date))),
AND dateadd(day, -DAY(getdate()), CAST(getdate() as date))
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Result of Query
(No column name) (No column name)
2011-03-01 19:31:07.500 2011-03-31 19:31:07.500