简体   繁体   中英

select all years that greater than 5 years ago from year today

 a          b
 1          2009
 2          2007
 3          2006
 4          2010
 5          2011

How I will select all years that greater than 5 years ago from year today?

So it would be like 2013(year today) - 5 = 2008
 a          b
 1          2009
 4          2010
 5          2011

I tried this:

select * from table1 where b > CURRENT_TIMESTAMP - 5

I use smallint as datatype of column b instead of date because I will only store the year. Is it safe to use small int for storing year?

您接近:

select * from table1 where b > YEAR(CURRENT_TIMESTAMP) - 5
select * from table1 where b > DATEADD(YEAR,-5,GETDATE())

应该也可以

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM