List of years in SQL Server

Here is a CTE from my friend Phil that returns a list of years in an efficient manner. Good reference.

/*Return a list of years since 2009*/

WITH yearlist
AS ( SELECT 2009 AS year
UNION ALL
SELECT yl.year + 1 AS year
FROM yearlist yl
WHERE yl.year + 1 <= YEAR(GETDATE())
)
SELECT year
FROM yearlist
ORDER BY year DESC;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s