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 |
|
helixpoint
Constraint Violating Yak Guru
291 Posts |
Posted - 2009-10-07 : 14:31:29
|
| I have an integer field that is a year/month. The data looks like "200901" I need to be able to select all the values in there that are 25 momths back. So there could be 200701 , 200704, 200712, 200802DaveHelixpoint Web Developmenthttp://www.helixpoint.com |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-10-07 : 14:51:50
|
| Yoicks! Try thisdeclare @ym intdeclare @today datetimeset @today = dateadd(month,datediff(month,0,getdate()),0)select @todayset @ym = 200901 select * from yourTablewhere convert(datetime,convert(char(10), @ym*100+1)) >= dateadd(month,-25,@today)JimEveryday I learn something that somebody else already knew |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-10-07 : 15:11:33
|
Here is an alternate method that can use an index on your Year/Month column, should one exist:DECLARE @YearMonth INTSET @YearMonth = (YEAR(DATEADD(MONTH, -25, CURRENT_TIMESTAMP)) * 100) + MONTH(DATEADD(MONTH, -25, CURRENT_TIMESTAMP))SELECT *FROM MyTableWHERE MyColumn >= @YearMonth |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-10-07 : 15:20:18
|
SELECT * FROM Table1 WHERE Col1 >= CAST(CONVERT(CHAR(6), DATEADD(MONTH, -25, GETDATE()), 112) AS INT) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|
|
|