Dibstar
Dibstar

Reputation: 2364

Using declared variables as column names

I'm looking for a way to use a dynamic variable as a column name as well - for example if I hypothetically use the following to define a financial year:

DECLARE @currentfy NVARCHAR(6) --Current financial year
SET @currentfy = YEAR(GETDATE()) - CASE WHEN MONTH(GETDATE()) < 4 THEN 1 ELSE 0 END

I then want to be able to do something like this:

SELECT @currentfy AS @currentfy
SELECT @currentfy - 1 AS @currentfy_1

So that it looks like as if I had done this:

SELECT 2010 AS [2010]
SELECT 2009 AS [2009]

Is there a way to do this without using dynamic pivoting? (as my tables are large and I want to avoid pivoting if possible).

Upvotes: 0

Views: 270

Answers (1)

gbn
gbn

Reputation: 432200

No, use dynamic pivoting or an extra column/resultset to describe the subsequent columns/resultset

Upvotes: 1

Related Questions