Get End of Month

The following short code will return the date and time for the very end of the month passed in. It works by first getting the beginning of the month for the date passed in, adding one month to that date, then subtracting 3 milliseconds. This gives the latest possible date for the end of the month as the DateTime data type has a granularity of 3 milliseconds.

[cc lang=”sql”]
DECLARE @DateIn datetime

SET @DateIn = GETDATE()

SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,@DateIn)+1,0)))
[/cc]

Featured Articles

 Site Author

  • Thanks for visiting!
css.php