Link to home
Start Free TrialLog in
Avatar of nitayalevette
nitayalevetteFlag for United States of America

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(day,getdate()) ),getdate()) )
Avatar of Alpesh Patel
Alpesh Patel
Flag of India image

It comes with Time stamp. so please remove timestamp from the Query.

Result of Query

(No column name)      (No column name)
2011-03-01 19:31:07.500      2011-03-31 19:31:07.500
DateOfOrder BETWEEN convert( datetime, -datediff(day,dateadd(month,-1,getdate()),day(getdate())-1) )
    AND convert( datetime, -datediff(day,getdate(),day(getdate())) )
SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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))

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial