I need a list of years as a recordset starting with 2004 to current year (in desc order), without writing a stored procedure. Is this possible? (SQL Server 2005). So it should return:
2009
2008
2007
2006
2005
2004
I need a list of years as a recordset starting with 2004 to current year (in desc order), without writing a stored procedure. Is this possible? (SQL Server 2005). So it should return:
2009
2008
2007
2006
2005
2004
Try this:
declare @lowyear int
set @lowyear = 2004
declare @thisyear int
set @thisyear = year(getdate())
while @thisyear >= @lowyear
begin
print @thisyear
set @thisyear = (@thisyear - 1)
end
Returns
2009
2008
2007
2006
2005
2004
When you hit Jan 1, 2010. The same code will return:
2010
2009
2008
2007
2006
2005
2004
I think you need to create a dates table, then just select your range from it. It can also come in useful when you need to select a date range with X data attached and not have any missed days.
Replace AnyTable with a table with at least the amount of rows as the amount of years you're asking. Replace AnyColumn with a column from the table.
SELECT *
FROM (
SELECT TOP 100 2003 + ROW_NUMBER() OVER (ORDER BY <AnyColumn>) AS Yr
FROM dbo.<AnyTable>
) Years
WHERE Yr <= YEAR(GETDATE())
Updated to return current year plus previous 5 years. Should be very fast as this is a small recordset.
SELECT YEAR(GETDATE()) as YearNum
UNION
SELECT YEAR(GETDATE()) - 1 as YearNum
UNION
SELECT YEAR(GETDATE()) - 2 as YearNum
UNION
SELECT YEAR(GETDATE()) - 3 as YearNum
UNION
SELECT YEAR(GETDATE()) - 4 as YearNum
UNION
SELECT YEAR(GETDATE()) - 5 as YearNum
ORDER BY YearNum DESC
DECLARE @YEARS TABLE (Y INT)
DECLARE @I INT, @NY INT
SELECT @I = 2004, @NY = YEAR(GETDATE())
WHILE @I <= @NY BEGIN
INSERT @YEARS SELECT @I
SET @I = @I + 1
END
SELECT Y
FROM @YEARS
ORDER BY Y DESC
Any particular reason a table-valued UDF is unacceptable? Do something like this otherwise.
SET NOCOUNT ON
DECLARE @max int
set @max = DATEPART(year, getdate())
CREATE TABLE #temp (val int)
while @max >= 2004
BEGIN
insert #temp(val) values(@max)
set @max = @max - 1
END
SELECT * from #temp