Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
This function returns the defined length of a column, in bytes.
Transact-SQL syntax conventions
COL_LENGTH ( 'table' , 'column' )
' table '
The name of the table whose column length information we want to determine. table is an expression of type nvarchar.
' column '
The column name whose length we want to determine. column is an expression of type nvarchar.
smallint
Returns NULL on error, or if a caller does not have the correct permission to view the object.
In SQL Server, a user can only view the metadata of securables that the user owns, or on which the user has been granted permission. This means that metadata-emitting, built-in functions such as COL_LENGTH might return NULL, if the user does not have correct permission on the object. See Metadata Visibility Configuration for more information.
For varchar columns declared with the max specifier (varchar(max)), COL_LENGTH returns the value -1.
This example shows the return values for a column of type varchar(40)
and a column of type nvarchar(40)
:
USE AdventureWorks2022;
GO
CREATE TABLE t1(c1 VARCHAR(40), c2 NVARCHAR(40) );
GO
SELECT COL_LENGTH('t1','c1')AS 'VarChar',
COL_LENGTH('t1','c2')AS 'NVarChar';
GO
DROP TABLE t1;
Here's the result set.
VarChar NVarChar
40 80
Expressions (Transact-SQL)
Metadata Functions (Transact-SQL)
COL_NAME (Transact-SQL)
COLUMNPROPERTY (Transact-SQL)
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today