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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.