Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get the last day of previous month?? and last day of prior two months?
means when i give getdate() i need feb lastday and january lastday also..

What I have tried:

I tried but iam not sure how to get it.
Posted
Updated 10-Mar-16 18:08pm

Hello,
Refer link :
C#
SQL SERVER – Find Last Day of Any Month – Current Previous Next



In Short
SQL
----Last Day of Month before Previous Month 
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0))

 
----Last Day of Previous Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))

----Last Day of Current Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))

----Last Day of Next Month
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))
 
Share this answer
 
v2
Comments
Member 11931581 10-Mar-16 3:39am    
Hi Thanks alot!! and I have a small doubt ..I hiis code what is mean by 's'(SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)))
jaket-cp 10-Mar-16 4:25am    
It would be good if you could do an In Long version of your solution
Animesh Datta 10-Mar-16 4:57am    
Hello ,
Here 's' is an Abbreviations of datepart . for more information check
https://msdn.microsoft.com/en-us/library/ms186819.aspx .

If your problem is solved then Accept the answer.
jaket-cp 10-Mar-16 5:03am    
Thanks for that, but I understand what is happening but the op does not.
I would also give links to DateDiff and GetDate.
And probably explain in pseudocode code what is happening, that would be a improved solution and I would give you a 5star for that :)
Member 11931581 10-Mar-16 5:10am    
My Problem Solved..Thanks a lot!!Animesh..
SQL
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900