Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a query that returns consecutive years?

Status
Not open for further replies.

sjh

Programmer
Oct 29, 2001
263
US
Hi,

I want to create a query that returns 15 rows of consecutive years. The value of the first year would be entered by a user. (i.e. if the user entered 2004, it would return 15 rows starting with 2004 upto 2018)

Is there a way to do this in SQL?

Thank you in advance for your help!
SJH
 
You are wanting a loop within a SQL query and transactional SQL does not cope with that. You could easily write a function to manage this or you could do get the result by playing dirty eg create a new table, countTable, with a single field countID populated with 15 fields starting at 0 up to 14. Then query your table and the user input thus:

SELECT year + countid
FROM Count_Table
ORDER BY CountID;

where year is your users input value. From this you get 15 records from 2004 to 2018.

Snowcrash
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top