Posted By

sjmoorecouk on 08/10/12


Tagged

sql server Microsoft


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

johnw


Return last day of month


 / Published in: SQL
 

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

  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  

You need to login to post a comment.