COL_NAME (Transact-SQL)
Returns the name of a column.
COL_NAME ( [ database_id , table_id , column_id ] )
Syntax
COL_NAME returns the name of a column. It can be used to retrieve the column name for a given table and column ID.
SELECT COL_NAME(OBJECT_ID('YourTableName'), column_id)
FROM sys.columns
WHERE object_id = OBJECT_ID('YourTableName') AND column_id = 1;
Arguments
Argument | Description | Possible values |
---|---|---|
database_id |
The ID of the database. If omitted, the current database is assumed. | An integer value representing the database ID. |
table_id |
The ID of the table. | An integer value representing the table ID. Use OBJECT_ID to get the table ID. |
column_id |
The ID of the column. | An integer value representing the column ID. |
Return Type
sysname (alias for nvarchar(128))
Permissions
Requires VIEW DEFINITION permission on the database.
Examples
Example 1: Get the name of a column by ID
The following example returns the name of the column with ID 1 in the 'Person' table in the current database.
SELECT COL_NAME(OBJECT_ID('Person'), 1);
Example 2: Get all column names for a table
This example retrieves the names of all columns in the 'Employee' table.
SELECT
c.name AS column_name
FROM
sys.columns AS c
WHERE
c.object_id = OBJECT_ID('Employee');
Tip: You can also use COL_LENGTH to get the length of a column.