2

How can I display only the specfic columns from INFORMATION_SCHEMA.COLUMNS schema?

Ex:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'rate_Structure'
ORDER BY ORDINAL_POSITION

Returns:

+-------------+
|             |
| COLUMN_NAME |
+-------------+
|    a        |
|    b        |
|    c        |
|    d        |
|    e        |
+-------------+

How to show only a and e columns?

2
  • 1
    Possible duplicate of Select specific rows and columns from an SQL database Commented Oct 27, 2018 at 8:46
  • You should learn how SQL works. This is a query like any other, so you filter the results the same way you filter results for any other query. Commented Oct 27, 2018 at 9:50

4 Answers 4

2

how to Show only a and e columns

Simply, by filtering the results in WHERE clause as

SELECT COLUMN_NAME    
FROM INFORMATION_SCHEMA.COLUMNS    
WHERE TABLE_NAME = 'rate_Structure' and COLUMN_NAME in ('a','e') 
-- You can also use: and (COLUMN_NAME = 'a' or COLUMN_NAME = 'e')    
ORDER BY ORDINAL_POSITION

You need to add the condition to get what you need, and COLUMN_NAME in ('a','e') will return only COLUMN_NAME has 'a' or 'e' value.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to add aditional conditions like:

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'rate_Structure'  
  AND data_type = 'int'    -- condition to get only specific columns
ORDER BY ORDINAL_POSITION;

1 Comment

The OP want a and e columns no matter the data type
1

Add another condition in where

SELECT COLUMN_NAME    
FROM INFORMATION_SCHEMA.COLUMNS    
WHERE TABLE_NAME = 'rate_Structure' and COLUMN_NAME in ('a','c')    
ORDER BY ORDINAL_POSITION

1 Comment

It's e column instead of c
0

You can use INFORMATION_SCHEMA.COLUMNS as below

USE Test
GO
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'SalesTerritory'
AND COLUMN_NAME = 'Region'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.