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.
Author |
Topic |
cidr
Posting Yak Master
207 Posts |
Posted - 2009-11-04 : 17:28:54
|
Hi there,Does anyone know how to take a date and seperate the month and year and then subtract a month? for exampleTake: 01 Mar 2009Make: 03 2009Then subtract a month: 02 2009I keep getting conversion errors. Any help would be greatCheers |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2009-11-04 : 18:11:28
|
[code]select -- Create formatted string: MM YYYY MM_YYYY = right(replace(convert(varchar(30),DT,103),'/',' '),7)from ( Select -- Subtract one month DT = dateadd(month,-1,DT) from ( -- Convert string to Datetime select Dt = convert(datetime,'01 Mar 2009') ) aa ) a[/code]Results:[code]MM_YYYY ------- 02 2009[/code]CODO ERGO SUM |
|
|
cidr
Posting Yak Master
207 Posts |
Posted - 2009-11-05 : 16:54:12
|
Thanks Michael, that worked a treatCheersPaul |
|
|
|
|
|