In this blog post, I will be sharing a new date feature in SQL Server 2012 called the EOMONTH (End of Month) which is used to find or get the last day of a month in a specified date.
In prior versions of SQL Server 2012, date functions like “dateadd” and/or “datediff” were invaluable in finding the last day of a month.
Consider a SQL Server 2005/2008 R2 example of how we find the last day of a month;
The use of the date functions in the example above returns a date value of 2012-08-31 00:00:00.000 (see red arrow).
Now this same result can be achieved by using the EOMONTH function in SQL Server 2012 as shown below;
DECLARE @date DATETIME
SET @date = ‘20120802’
SELECT EOMONTH (@date) AS LAST_DAY_OF _MONTH
This returns ‘2012-02-31’
Thanks for reading and hope you’ve gained something.
cheers.
For additional reading, please see http://msdn.microsoft.com/en-us/library/hh213020.aspx