Return to Snippet

Revision: 58923
at August 10, 2012 00:10 by sjmoorecouk


Initial Code
DECLARE @StringDate varchar(20) -- string variable used to hold date in string format
DECLARE @StringDay varchar(2)   -- string variable used to hold day number

-- work out what the last day of the month is
SELECT @StringDay=
CASE 
WHEN @MonthNumber IN(1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN @MonthNumber IN(4, 6, 9, 11) THEN 30
ELSE 29
END

BEGIN TRY
SET @StringDate=@YearNumber + '-' +  '02-' + @StringDay
SET @StartDate = CONVERT(DateTime, @StringDate)
END TRY
BEGIN CATCH
SET @StringDay='28'
END CATCH

Initial URL


Initial Description
Input: Month and year number
Output: Last day of month

Initial Title
Return last day of month

Initial Tags
sql, server

Initial Language
SQL