to get last date of Previous month in sqlserver 2008

Posted by Manoj_B under Sql Server on 12/19/2012 | Points: 10 | Views : 2473 | Status : [Member] | Replies : 3
hii,


i've doubt to get last date of Previous month in sqlserver 2008.

i've used

"select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-1900,0)))"

it returns current last date of curent month,
i want last date of previous month by giving month and year.

Plase help me. it's urgent.

Thanks & Regards
Manoj Kumar



Responses

Posted by: Naraayanan on: 12/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
try this Code
select DATEADD(day,-1,DATEADD(month,6-1,DATEADD(year,2010-1900,0)))


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

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

Posted by: Sandeepmhatre on: 12/19/2012 [Member] Starter | Points: 25
Posted by: Pandians on: 12/19/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Check It Out!
Declare @CDate Datetime, @Year Varchar(4), @Month Varchar(2)


Select @Year = 2012,
@Month = 12

Select @CDate = @Year + '-' + @Month + '-01'
Select @CDate - DAY(@CDate) [Last date of previous month]
Result
Last date of previous month

----------------------------
2012-11-30 00:00:00.000


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

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

Login to post response