Tuesday, December 3, 2013

Calculate the dates of previous, current and next month

--SQL Server 2005/2008/2012
DECLARE @DATE DATETIME
 
Select @DATE = GetDate()
 
--First date of the Previous Month
SELECT CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,@DATE)-1,0),120) AS [Previous Month]
 
--First date of the Current Month
SELECT CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,@DATE),0),120) AS [Current Month]
 
--First date of the Next Month
SELECT CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,@DATE)+1,0),120) AS [Next Month]
 
Previous Month
--------------
2013-11-01
 
(1 row(s) affected)
 
Current Month
-------------
2013-12-01
 
(1 row(s) affected)
 
Next Month
----------
2014-01-01
 
(1 row(s) affected)

No comments:

Post a Comment