fcsIT
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())
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())
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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))
select distinct cpid
from results
where eventdate >= DATEADD(d,-1 * DAY(getdate()) + 1,cast(FLOOR(cast(getdate(
ASKER
Perfect, thanks!
from results
where eventdate > DATEADD(d,-1 * DAY(getdate()) + 1,getdate())