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'