Using the DATEADD
and DATEDIFF
functions, it's possible to return the last date of a month.
SELECT DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, '2016-09-23') + 1, 0))
-- 2016-09-30 00:00:00.000
The EOMONTH
function provides a more concise way to return the last date of a month, and has an optional parameter to offset the month.
SELECT EOMONTH('2016-07-21') --2016-07-31
SELECT EOMONTH('2016-07-21', 4) --2016-11-30
SELECT EOMONTH('2016-07-21', -5) --2016-02-29