In SQL Server, EOMONTH
the function is used to return the date of the last day of the month in which the specified date falls. Its syntax is as follows:
EOMONTH ( start_date [, month_to_add ] )
where start_date
is a date expression specifying the date to be calculated. month_to_add
is an optional parameter that specifies start_date
the number of months to add to.
select EOMONTH(GETDATE()) AS '本月最后一天'
select EOMONTH(GETDATE(),1) AS '下月最后一天'
select EOMONTH(GETDATE(),-1) AS '上月最后一天'
result:
It should be noted that EOMONTH
functions were only introduced in SQL Server 2012 and above. In earlier versions, some functions and expressions could be used to calculate the last day of a month, such as DATEADD
, , DAY
and DATEPART
etc.