Calculate the Last Day of the Month using SQL
Calculate the Last Day
Today I needed to calculate the last day of the previous month for a Customer.
Ever heard from the function EOMONTH?
Searching on the web I came across a function I never heard from before EOMONTH, this function can be used as of SQL Server 2012.
With this function you never have to worry anymore about how many days(28,29,30,31) your month has.
The function works as follows:
declare @datum datetime = ‘2018.11.01’
SELECT EOMONTH(DATEADD(mm, -1, @datum)) as LastDayPreviousMonth
SELECT EOMONTH(DATEADD(mm, -1, getdate())) as LastDayPreviousMonth
SELECT EOMONTH(getdate()) as LastDayCurrentMonth