Find First and Last Day of Current Month in SQL Server

Find First and Last Day of Current Month in SQL Server

Hello everyone,  I am going to share the code sample to get the first and last day of current month using  SQL Server. The code detail as given below.

DECLARE @GETDATE DATETIME
SELECT @GETDATE = GETDATE()

--QUERY FOR GET LAST DAY OF PREVIOUS MONTH
SELECT CONVERT(VARCHAR(25),DATEADD(DD,-(DAY(@GETDATE)),@GETDATE),105) AS MY_DATE ,'LAST DAY OF PREVIOUS MONTH' AS MY_DATE_TYPE

UNION ALL

--QUERY FOR GET FIRST DAY OF CURRENT MONTH
SELECT CONVERT(VARCHAR(25),DATEADD(DD,-(DAY(@GETDATE)-1),@GETDATE),105) AS DATE_VALUE,'FIRST DAY OF CURRENT MONTH'

UNION ALL

--QUERY FOR GET TODAY
SELECT CONVERT(VARCHAR(25),@GETDATE,105) AS DATE_VALUE, 'TODAY'

UNION ALL

--QUERY FOR GET LAST DAY OF CURRENT MONTH
SELECT CONVERT(VARCHAR(25),DATEADD(DD,-(DAY(DATEADD(MM,1,@GETDATE))),DATEADD(MM,1,@GETDATE)),105) ,'LAST DAY OF CURRENT MONTH'

UNION ALL

--QUERY FOR GET FIRST DAY OF NEXT MONTH
SELECT CONVERT(VARCHAR(25),DATEADD(DD,-(DAY(DATEADD(MM,1,@GETDATE))-1),DATEADD(MM,1,@GETDATE)),105) ,'FIRST DAY OF NEXT MONTH'


The output : go to below image


ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.