How to get few column names among all columns in the one table with query in sql server 2008

sagar patil
select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME='Submenu';

The above query gave all column names in Submenu table but I want only three column names of Submenu table.

Is there any way to get column names?

SchmitzIT

I assumed this is SQL Server, so the below queries might not work on other RDBMS's).

If your question is how to find only the column names you need (presuming you know in advance which ones those are), you would have to do something like this:

SELECT 
    COLUMN_NAME
FROM
    INFORMATION_SCHEMA.COLUMNS 
WHERE 
    TABLE_NAME='Submenu'
    AND COLUMN_NAME IN ('Column1', 'Column2', 'Column3')

At this moment, you basically are requesting a list of any column within your table, without any restrictions whatsoever.

Alternatively, if you're looking only for the first three column names, this would work:

SELECT TOP 3
    COLUMN_NAME
FROM
    INFORMATION_SCHEMA.COLUMNS 
WHERE 
    TABLE_NAME='Submenu'
ORDER BY
    ORDERINAL_POSITION

In the latter case, you will have to determine how you want to sort the column names though (either by using something like ORDER BY COLUMN_NAME, in case you want them listed alphabetically, or ORDER BY ORDERINAL_POSITION in case you're trying to get them in the order they appear in the table).

If this is not what you meant, please elaborate on what you are trying to achieve.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get all column names without one column in sql table in SQL Server

From Dev

How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

From Dev

How to get all columns from first table when there is no matching column in SQL Server 2012

From Dev

How to get all columns from first table when there is no matching column in SQL Server 2012

From Java

How can I get column names from a table in SQL Server?

From Dev

Need to print all the missing value column names in SQL Server 2008

From Dev

Need to print all the missing value column names in SQL Server 2008

From Dev

SQL query to get a calculated column based on few conditions on existing columns

From Dev

SQL Server 2008 join multiple columns to form one column

From Dev

SQL server 2008 query move table data from one to another

From Dev

How to Get Sum of One Column Based On Other Table in Sql Server

From Dev

How to get all columns from one table and only one column from another table with ID ? - MySql

From Dev

How I can get table definition in SQL SERVER 2008 R2 using SQL query?

From Dev

How do I use SELECT on columns that don't have column names in SQL Server 2008 R2?

From Dev

Drop all columns in a table if their names follow a specific pattern in SQL Server

From Dev

How to extract table names and column names from sql query?

From Dev

How to get a view table query (code) in SQL Server 2008 Management Studio

From Dev

How to change few column names in a data table

From Dev

SQL Server Table Transformation Query / Combine Column Names With Values

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information

From Dev

SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information

From Dev

SQL Query How to Find Data for Last Few Months in January (mixed years) SQL Server 2008

From Dev

In SQL Server, how do I retrieve data values from table column names identified in a sub-query?

From Dev

How to Update, Insert, Delete in one MERGE query in Sql Server 2008?

From Dev

How to find if there is only one hyphen in a column SQL Server 2008?

From Dev

How to find if there is only one hyphen in a column SQL Server 2008?

From Dev

How to join CTE query with another table in SQL Server 2008

From Dev

How to copy all the data from a table in Sql Server 2008 to file

Related Related

  1. 1

    How to get all column names without one column in sql table in SQL Server

  2. 2

    How to get list of constraints of a table along with their respective column names in SQL Server 2008 R2

  3. 3

    How to get all columns from first table when there is no matching column in SQL Server 2012

  4. 4

    How to get all columns from first table when there is no matching column in SQL Server 2012

  5. 5

    How can I get column names from a table in SQL Server?

  6. 6

    Need to print all the missing value column names in SQL Server 2008

  7. 7

    Need to print all the missing value column names in SQL Server 2008

  8. 8

    SQL query to get a calculated column based on few conditions on existing columns

  9. 9

    SQL Server 2008 join multiple columns to form one column

  10. 10

    SQL server 2008 query move table data from one to another

  11. 11

    How to Get Sum of One Column Based On Other Table in Sql Server

  12. 12

    How to get all columns from one table and only one column from another table with ID ? - MySql

  13. 13

    How I can get table definition in SQL SERVER 2008 R2 using SQL query?

  14. 14

    How do I use SELECT on columns that don't have column names in SQL Server 2008 R2?

  15. 15

    Drop all columns in a table if their names follow a specific pattern in SQL Server

  16. 16

    How to extract table names and column names from sql query?

  17. 17

    How to get a view table query (code) in SQL Server 2008 Management Studio

  18. 18

    How to change few column names in a data table

  19. 19

    SQL Server Table Transformation Query / Combine Column Names With Values

  20. 20

    SQL Query to compare two columns with one column from another table (and get two values)

  21. 21

    SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information

  22. 22

    SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information

  23. 23

    SQL Query How to Find Data for Last Few Months in January (mixed years) SQL Server 2008

  24. 24

    In SQL Server, how do I retrieve data values from table column names identified in a sub-query?

  25. 25

    How to Update, Insert, Delete in one MERGE query in Sql Server 2008?

  26. 26

    How to find if there is only one hyphen in a column SQL Server 2008?

  27. 27

    How to find if there is only one hyphen in a column SQL Server 2008?

  28. 28

    How to join CTE query with another table in SQL Server 2008

  29. 29

    How to copy all the data from a table in Sql Server 2008 to file

HotTag

Archive