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

Set Begin and End Dates

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
<br>I need help on how to set a begin date to the first of a month (from GETDATE ()) and then set the enddate to the end of the month.<br>Thank you....
 
Use DATEPART(yyyy,GETDATE()) to get the integer year<br>Use DATEPART(mm,GETDATE()) to get the integer month<br>Use CONVERT(char(4),DATEPART(yyyy,GETDATE())) to get the year in a string format<br>Use CONVERT(char(2),DATEPART(mm,GETDATE())) to get the month in a string format<br>The day is always 01, so put it all together as<br><br>declare @BeginDate datetime<br>declare @EndDate datetime<br>Set @BeginDate = CONVERT(char(4),DATEPART(yyyy,GETDATE())) + '/' + CONVERT(char(2),DATEPART(mm,GETDATE())) + '/' + '01'<br>As the end of the month varies in number, one thing that is always true is it is the day before the first day of the next month.<br>So, add a month to the begin date, and subtract a day.<br>Set @EndDate = DATEADD(day, -1, (DATEADD(month, 1, @BeginDate)))<br><br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top