How to select specific column names in SQL Server

user1987631 picture user1987631 · Feb 27, 2013 · Viewed 10.8k times · Source

Here is my previous question on how to iterate a string in SQL Server:

Now how can I specifically select column names? here is my code for selecting columns:

SELECT 'Field '+CAST(ROW_NUMBER() OVER (ORDER BY ordinal_position) AS varchar(5))+': ' + 
COLUMN_NAME
FROM information_schema.columns 
WHERE table_Name = 'SystemDefined' and table_schema = 'schemaAsset'

Here is the output:

Field 1: Asset_No
Field 2: AssetCategory
Field 3: AssetClassification
Field 4: PurchaseType
Field 5: Department
Field 6: RespPerson
Field 7: Status
Field 8: Location

This the output I want when selecting specific column names:

Field 1: Asset_No
Field 2: AssetCategory
Field 3: AssetClassification
Field 4: PurchaseType
Field 5: Department
Field 6: RespPerson
Field 7: Status

Answer

John Woo picture John Woo · Feb 27, 2013

How about using NOT IN which you can use on the WHERE clause to specify for another condition.

SELECT...
FROM..
WHERE  table_Name = 'SystemDefined' AND 
       table_schema = 'schemaAsset' AND
       COLUMN_NAME NOT IN ('Status',....) --  specify the list of names you
                                          -- don't want to show