Reputation: 455
I need to get last day of last month in SQL Server in the format "2017-05-31". Does anyone know how to write query for this?
Upvotes: 1
Views: 7918
Reputation: 1269573
There isn't a SQL Server 2010 (to my knowledge). In SQL Server 2012+, you can use EOMONTH()
:
select eomonth(dateadd(month, -1, getdate())
Actually, in any version, it is probably simpler to just do:
select dateadd(day, -day(getdate()), getdate())
Oh, and then cast to a date to get rid of the time component:
select cast(dateadd(day, -day(getdate()), getdate()) as date)
Upvotes: 7