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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1st and last day of month 1

Status
Not open for further replies.

GShen

MIS
Joined
Sep 26, 2002
Messages
561
Location
US
Is there an easy way (function) other than creating a long string with datepart to get the 1st and last day of a prior month ? Assuming this job runs the 1st day of every month.
Ex. 3/1/10, I need a start date of 02/01/10 and an end date of 02/28/10.
Ex. 01/01/10, I need a start date of 12/1/09 and an end date of 12/31/09.

I am doing a long string manipulation to get this, just wondering if there were any reserved words to pick this off more efficiently.



Remember when... everything worked and there was a reason for it?
 
if you are certain that the job will always run on the first day of the month, then DATEADD(m,-1,GETDATE()) will give you the first day of the previous month, and DATEADD(d,-1,GETDATE()) will give you the last day of the previous month, with both expressions safe for use across year boundaries

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
r937,
Thanks! I thought I did something like this somewhere and I couldn't and still can't find it. This will work perfectly. Yes, it is in a job scheduled to be run on the 1st day of every month.
Much easier to read than the nonsense I was creating.

Remember when... everything worked and there was a reason for it?
 
Look at the FAQ section for this forum

djj
The Lord is My Shepard (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top