简体   繁体   中英

T-SQL : Get Current + Previous years from date table

I have year values from 2009 to 2015 in my date table. I only want to get years from Min(year) to Current(year).

How can I achieve this?

Thank you

只需选择“小于或等于当前年份”的thouse

select * from table where datepart(year, date) <= datepart(year, getdate())

I think something like this is what you're looking for:

SELECT *
  FROM yourTable
  WHERE year BETWEEN (SELECT MIN(year) FROM yourTable) AND DATEPART(year, GETDATE())

Or, if you don't have years but instead full dates:

SELECT *
  FROM yourTable
  WHERE theDate BETWEEN (SELECT DATEPART(year, MIN(theDate)) FROM yourTable) AND DATEPART(year, GETDATE())

I think the simplest is:

select *
from t
where YearValue <= year(getdate())

The minimum value takes care of itself -- you have no data before the minimum year.

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