Query to find Last day of the current month

Posted by Kirthiga under Sql Server on 5/23/2018 | Points: 10 | Views : 1172 | Status : [Member] | Replies : 2
How to find last day of the current month in sql server




Responses

Posted by: Rajnilari2015 on: 5/23/2018 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

select 
EOMONTH(getdate()), -- sql server 2012 onwards
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, getdate()) + 1, 0)) -- any version of sql


Hope this helps

--
Thanks & Regards,
RNA Team

Kirthiga, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Purvi on: 7/3/2019 [Member] Starter | Points: 25

Up
0
Down
hi !

first of all

step 1:
Get the DateTime of Now
 GETDATE() 
step 2:

Calculate the difference in month's from '1900-01-01'
DATEDIFF(m, 0, GETDATE()) 
step 3:
Add the difference to '1900-01-01' plus one extra month
DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0) 
step 4:
Remove one second
DATEADD(s, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0)) -- 2011-09-30 23:59:59.000


Kirthiga, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response