Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 TAKE A MONTH AWAY FROM A GIVEN DATE

Author  Topic 

cardgunner

326 Posts

Posted - 2008-06-06 : 11:30:05
I've been at this awhile and I can't figure it out.

I need to take a month off a certain date.
declare @month int
declare @year int
set @month=1
set @year=2008

select cast(cast(@month as varchar) +'/01/'+ cast(@year as varchar) as datetime) start_date

start_date
------------------------------------------------------
2008-01-01 00:00:00.000

(1 row(s) affected)


What I need is how to figure an end_date that is one month less then the start date.







CardGunner

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-06 : 11:40:16
[code]declare @month int
declare @year int
set @month=1
set @year=2008

select dateadd(month,@month-1,dateadd(year,@year-1900,0)) as start_date,
dateadd(month,@month-2,dateadd(year,@year-1900,0)) as end_date
[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

cardgunner

326 Posts

Posted - 2008-06-06 : 12:01:50
That works perfectly.

Thanks again for your help.

2008-1990 = 108 yearly integers of 0 which then is 1/1/2008 then that is the date for the next part which is 1-2= -1 monthly integers of 01/01/2008 which is 12/01/2007.

How much of this is stuff you picked up vs. stuff you stayed up thinking about for days without sleep? LOL.

Again thanks for being that good.


CardGunner
Go to Top of Page
   

- Advertisement -