Friday, February 17, 2012

Date calculation

Hi,
I am simply looking for a calculation to get back always the last day of the month. Can anybody help me ?Originally posted by dajm
Hi,

I am simply looking for a calculation to get back always the last day of the month. Can anybody help me ?

declare @.month int,
@.year int
select @.month = 2,
@.year = 2000
select dateadd(dd,-1,convert(datetime,convert(varchar,(@.month+1))+ '/01/'+convert(varchar,@.year)))

I assume you must be passing the year & month to get the last day of the month. The above will work for that|||Must'nt forget about december

DECLARE @.x datetime
SELECT @.x = GetDate()
SELECT DATEADD(mm,1,CONVERT(datetime,CONVERT(varchar(2),D ATEPART(mm,@.x))+'/01/'+CONVERT(varchar(4),DATEPART(yy,@.x))))-1

What happens with this?

declare @.month int,
@.year int
select @.month = 12,
@.year = 2000
select dateadd(dd,-1,convert(datetime,convert(varchar,(@.month+1))+ '/01/'+convert(varchar,@.year)))

???|||Good point brett !!!
My Bad :)|||I dont seem to unserstand .. why all my posts are being posted in the duplicate today ?? i mean for every post i am making ... its being submitted twice|||Originally posted by Brett Kaiser
Must'nt forget about december

DECLARE @.x datetime
SELECT @.x = GetDate()
SELECT DATEADD(mm,1,CONVERT(datetime,CONVERT(varchar(2),D ATEPART(mm,@.x))+'/01/'+CONVERT(varchar(4),DATEPART(yy,@.x))))-1

What happens with this?

declare @.month int,
@.year int
select @.month = 12,
@.year = 2000
select dateadd(dd,-1,convert(datetime,convert(varchar,(@.month+1))+ '/01/'+convert(varchar,@.year)))

???

Sorry Brett, your function gives me back the same day of the last month. I am searching for something following: today is 2003/12/02 and what I need the get returned is 2003/11/30. How about this ? Any chance ?|||Sorry guys,

I am really dump !!!

All i needed is the following:

select getdate()-(datepart(dd,getdate() )

thx for your effort|||Originally posted by dajm
select getdate()-(datepart(dd,getdate() )


Brilliant !!!

No comments:

Post a Comment