Return last day of month


/ Published in: SQL
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. DECLARE @StringDate VARCHAR(20) -- string variable used to hold date in string format
  2. DECLARE @StringDay VARCHAR(2) -- string variable used to hold day number
  3.  
  4. -- work out what the last day of the month is
  5. SELECT @StringDay=
  6. CASE
  7. WHEN @MonthNumber IN(1, 3, 5, 7, 8, 10, 12) THEN 31
  8. WHEN @MonthNumber IN(4, 6, 9, 11) THEN 30
  9. ELSE 29
  10. END
  11.  
  12. BEGIN TRY
  13. SET @StringDate=@YearNumber + '-' + '02-' + @StringDay
  14. SET @StartDate = CONVERT(DateTime, @StringDate)
  15. END TRY
  16. BEGIN CATCH
  17. SET @StringDay='28'
  18. END CATCH

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.