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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return rows without querying a table/view 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
0
0
GB
What I'm trying to do is create a SQL query that internally generates the following result set without querying an actual table/view...
Code:
DESC         ABBR   CALENDAR_YEAR
Next Year    N      2013
This Year    C      2012
Last Year    L      2011
Older Year   O      2010

The respective values in the CALENDAR_YEAR column above can be generated using the following function calls...
Code:
YEAR(GETDATE())+1
YEAR(GETDATE())
YEAR(GETDATE())-1
YEAR(GETDATE())-2
...and I have a function that returns the appropriate letter shown in the ABBR column above when it is passed a date.

My question is: how do I actually formulate the query to return rows when I'm not interacting with a table/view?

My limited SQL ability only allows me to generate columns in a single row when not querying a table/view.

Any advice would be much appreciated.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Select 'Next Year' decr, 'N' abbr, YEAR(GETDATE())+1
union
Select 'This Year ' decr, 'C' abbr, YEAR(GETDATE())
union
Select 'Last Year' decr, 'L' abbr,YEAR(GETDATE())-1
Union
Select 'Older Year' decr, 'O' abbr,YEAR(GETDATE())-2
 
Perfect - thank you very much. I've never used UNION until now.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top