Monday, September 28, 2015

SQL - Get the Last Day of a month from a date

Original Post

http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/

Last Day of month for current Date

----Last Day of Previous MonthSELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))LastDay_PreviousMonth----Last Day of Current MonthSELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))LastDay_CurrentMonth----Last Day of Next MonthSELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))


Last Day of month for a specific date
--Last Day of Any Month and YearDECLARE @dtDate DATETIMESET @dtDate = '8/18/2007'SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))

No comments:

Post a Comment