SQL Server EOMONTH function

        In SQL Server, EOMONTHthe 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_dateis a date expression specifying the date to be calculated. month_to_addis an optional parameter that specifies start_datethe 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 EOMONTHfunctions 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, , DAYand DATEPARTetc. 

Guess you like

Origin blog.csdn.net/sinat_28984567/article/details/129645685