# Get Column Names from a Table in SQL Server

> Retrieve SQL Server column names using SELECT TOP 0, sp_columns, INFORMATION_SCHEMA.COLUMNS, or STRING_AGG to concatenate names into a single string

**URL:** https://sentry.io/answers/how-can-i-get-column-names-from-a-table-in-sql-server/

---

## The Problem

If you have a table in Microsoft SQL Server with multiple columns, how do you view their names quickly in a query? How do you see detailed information for each column? And how do you select and return them in a query, ideally in a single string?

## The Solution

Assume you have a table called Person with a few columns, like the following:

```sql
CREATE TABLE Person
(
    Id INT PRIMARY KEY,
    Name VARCHAR(255),
    Address VARCHAR(255),
    Description VARCHAR(255)
);
```

The fastest way to see the table columns is to run a query returning no rows:

```sql
SELECT TOP 0 * FROM Person;

-- Id|Name|Address|Description|
-- --+----+-------+-----------+
```

If you want to see detailed information on the columns, in cases when you might want to alter the table, there are two options. They return slightly different columns, with the second option having slightly more information. Pick whichever style you prefer.

Note that the `N` before a table name is optional. You need it only if your table name contains Unicode characters outside standard Latin letters.

Option 1:

```sql
EXEC sp_columns N'Person';

-- TABLE_QUALIFIER|TABLE_OWNER|TABLE_NAME|COLUMN_NAME|DATA_TYPE|TYPE_NAME|PRECISION|LENGTH|SCALE|RADIX|NULLABLE|REMARKS|COLUMN_DEF|SQL_DATA_TYPE|SQL_DATETIME_SUB|CHAR_OCTET_LENGTH|ORDINAL_POSITION|IS_NULLABLE|SS_DATA_TYPE|
-- ---------------+-----------+----------+-----------+---------+---------+---------+------+-----+-----+--------+-------+----------+-------------+----------------+-----------------+----------------+-----------+------------+
-- Test        |dbo        |Person    |Id         |        4|int      |       10|     4|    0|   10|       0|       |          |            4|                |                 |               1|NO         |          56|
-- Test        |dbo        |Person    |Name       |       12|varchar  |      255|   255|     |     |       1|       |          |           12|                |              255|               2|YES        |          39|
-- Test        |dbo        |Person    |Address    |       12|varchar  |      255|   255|     |     |       1|       |          |           12|                |              255|               3|YES        |          39|
-- Test        |dbo        |Person    |Description|       12|varchar  |      255|   255|     |     |       1|       |          |           12|                |              255|               4|YES        |          39|
```

Option 2:

```sql
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Person';

-- TABLE_CATALOG|TABLE_SCHEMA|TABLE_NAME|COLUMN_NAME|ORDINAL_POSITION|COLUMN_DEFAULT|IS_NULLABLE|DATA_TYPE|CHARACTER_MAXIMUM_LENGTH|CHARACTER_OCTET_LENGTH|NUMERIC_PRECISION|NUMERIC_PRECISION_RADIX|NUMERIC_SCALE|DATETIME_PRECISION|CHARACTER_SET_CATALOG|CHARACTER_SET_SCHEMA|CHARACTER_SET_NAME|COLLATION_CATALOG|COLLATION_SCHEMA|COLLATION_NAME              |DOMAIN_CATALOG|DOMAIN_SCHEMA|DOMAIN_NAME|
-- -------------+------------+----------+-----------+----------------+--------------+-----------+---------+------------------------+----------------------+-----------------+-----------------------+-------------+------------------+---------------------+--------------------+------------------+-----------------+----------------+----------------------------+--------------+-------------+-----------+
-- Test      |dbo         |Person    |Id         |               1|              |NO         |int      |                        |                      |               10|                     10|            0|                  |                     |                    |                  |                 |                |                            |              |             |           |
-- Test      |dbo         |Person    |Name       |               2|              |YES        |varchar  |                     255|                   255|                 |                       |             |                  |                     |                    |iso_1             |                 |                |SQL_Latin1_General_CP1_CI_AS|              |             |           |
-- Test      |dbo         |Person    |Address    |               3|              |YES        |varchar  |                     255|                   255|                 |                       |             |                  |                     |                    |iso_1             |                 |                |SQL_Latin1_General_CP1_CI_AS|              |             |           |
-- Test      |dbo         |Person    |Description|               4|              |YES        |varchar  |                     255|                   255|                 |                       |             |                  |                     |                    |iso_1             |                 |                |SQL_Latin1_General_CP1_CI_AS|              |             |           |
```

Finally, if you want to return the column names in one string, in case you want to display them in an app, you can concatenate them with the query below:

```sql
SELECT STRING_AGG(COLUMN_NAME, ', ') AS Columns
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Person';

-- Columns                       |
-- ------------------------------+
-- Id, Name, Address, Description|
```

---

*Source: [sentry.io/answers/how-can-i-get-column-names-from-a-table-in-sql-server/](https://sentry.io/answers/how-can-i-get-column-names-from-a-table-in-sql-server/)*
