Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends

I am facing one problem right now as " I want to give the value of a variable to the column name that i am going to select in a select statement ".
Examplly like
if i have a variable @date='01-2012'
then i want this value as a heading to my select statement
which is like
SELECT a , b ,c,
d AS @date
from table

So what i need is that i want @date Value to come as the heading of the select statement.

Thanks in advance .
Posted

1 solution

Instead of variable you can directly put the constant:

SQL
SELECT a , b ,c, d AS '01-2012' from table_name


or you need to execute the dynamic query:

SQL
declare @tdate varchar(20)
set @tdate = '01-2011'
exec('select sellerid, sellername as [' + @tdate + '] from sellers')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900