dwarf
dwarf

Reputation: 455

How can a column be renamed in results when getting columns from INFORMATION_SCHEMA?

So I have this:

CREATE PROCEDURE getBattingColumnNames
AS

SELECT COLUMN_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting' 
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO

And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:

SELECT        COLUMN_NAME.Doub AS 'DB'
FROM            INFORMATION_SCHEMA.COLUMNS
WHERE        (TABLE_NAME = 'Batting')

and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?

Upvotes: 1

Views: 677

Answers (1)

Andomar
Andomar

Reputation: 238116

You could use a case to translate the column name:

select  case COLUMN_NAME 
        when 'Doub' then 'DB'
        else COLUMN_NAME 
        end
from    ...

Upvotes: 3

Related Questions