Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data for previous month

Status
Not open for further replies.

cards4

Technical User
Dec 9, 2004
38
US
Hello,
I have a SQL statement that gets information from the previous month. It is scheduled to run every first of the month. However, if necessary, the SQL will need to be re-run after the first of the month but still get the same information. Currently, I have a dateadd(month, -1, getdate()) and dateadd(day, -1, getdate()) declaration to set the first and last day of the previous month. What can I do to get the same information if the SQL is run after the first of the month? Thank you.
 
--The first day and last day of any month just use a date for which month you want it to return
for example for February 2005 it would be

declare @d datetime
select @d ='20050214'
SELECT DATEADD(mm, DATEDIFF(mm, 0, @d)+0, 0) as FirstDay,
DATEADD(d,-1,DATEADD(mm, DATEDIFF(mm, 0, @d)+1, 0)) as LastDay

For September 2006 it would be
declare @d datetime
select @d ='20050914'
SELECT DATEADD(mm, DATEDIFF(mm, 0, @d)+0, 0) as FirstDay,
DATEADD(d,-1,DATEADD(mm, DATEDIFF(mm, 0, @d)+1, 0)) as LastDay



Denis The SQL Menace
SQL blog:
Personal Blog:
 
Would this still work if it ran on the first of the month?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top