Database Simplified Headline Animator

Database Simplified

Saturday, 1 October 2011

Finding Last Day Of Month In SQL Server Denali (MSSQL2012)

Calculating Last Day of Month For Any Given Date Has become more easy as EOMONTH (End Of Month) function has been introduced in SQL Server Denali (MSSQL 2011).

How ? See Following

Syntax :


EOMONTH ( start_date [, month_to_add ] )

Month_to_Add : This is Optional Parameter.


Select GetDate()As InputDate,EOMONTH (GETDATE()) As Result

Output:

image

How to Find Last Day Of Previous Month


Select GetDate()As InputDate,EOMONTH (GETDATE(),-1) As Result

image

So You Just Need To Add Optional Parameter Number_of_Month So To Get Previous Month Last Date I have Added –1 To Optional Parameter. To Get Desired Month Last Date You Can Pass No_Of_Month Parameter.

See Following Screen Shot

image

No comments:

Post a Comment