How to find Last Date of Every Month in SQL Server Code?

How to find Last Date of Every Month in SQL Server Code?


DECLARE @OutputDate DATETIME
DECLARE @InputDate DATETIME = '02/02/2000'
SET @OutputDate = CAST(YEAR(@InputDate) AS VARCHAR(4)) + '/' +
CAST(MONTH(@InputDate) AS VARCHAR(2)) + '/01'

SET @OutputDate = DATEADD(DD, -1, DATEADD(M, 1, @OutputDate))

SELECT CONVERT(VARCHAR(2),DATEPART(dd,@OutputDate))

No comments: