Link to home
Start Free TrialLog in
Avatar of fcsIT
fcsITFlag for United States of America

asked on

SQL query to return records from only the current month

I need to change the DATEADD portion of the following query so it looks at the first day of the current month forward, instead of simply the previous 30 days.

Example, using the query shown below, and today's date of April 17th, I need it to say DATEADD(d,-17,getdate())

How do I write a query that automatically calculates how many days back the first day of the current month is?

select distinct cpid
from results
where eventdate > DATEADD(d,-30,getdate())
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

select distinct cpid
from results
where eventdate > DATEADD(d,-1 * DAY(getdate()) + 1,getdate())
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland 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
or to use the date only and ignore time

select distinct cpid
from results
where eventdate >= DATEADD(d,-1 * DAY(getdate()) + 1,cast(FLOOR(cast(getdate() as float)) as datetime))
Avatar of fcsIT

ASKER

Perfect, thanks!