Hi @Sudip Bhatt ,
As mentioned by Guoxiong, you could create a relational table and create a dynamic statement to produce this select statement with expected alias.
You could refer below simple example:
--create tblModel table
create table tblModel
(
ID int,
Section int,
LineItem varchar(10),
F1 int,
F2 int,
F3 int,
F4 int
)
--insert one row of sample data into tblModel table
insert into tblModel values
(1,1,'Item1',10,20,30,40)
--create tblPeriods table
create table tblPeriods
(
[2010 FY] int,
[2011 FY] int,
[2012 FY] int,
[1Q 2013] int
)
--create relational table named tblPeriods
create table tblModelPeriods
(
RawName VARCHAR(20),
RealName VARCHAR(20))
insert into tblModelPeriods values
('F1','2010 FY'),
('F2','2011 FY'),
('F3','2012 FY'),
('F4','1Q 2013')
--produre this dynamic statement
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = STUFF(( SELECT ', ' + QUOTENAME(C.COLUMN_NAME) + ' AS ' + QUOTENAME(F.RealName)
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN tblModelPeriods F ON C.COLUMN_NAME = F.RawName
WHERE C.TABLE_SCHEMA = 'dbo' AND C.TABLE_NAME = 'tblModel'
ORDER BY C.ORDINAL_POSITION FOR XML PATH('')),1,2,'')
SELECT @SQL = 'SELECT ID,Section,LineItem,' + @SQL + ' FROM tblModel'
EXECUTE(@SQL)
Output:
ID Section LineItem 2010 FY 2011 FY 2012 FY 1Q 2013
1 1 Item1 10 20 30 40
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.