Skip to content

Latest commit

 

History

History
82 lines (64 loc) · 2.79 KB

File metadata and controls

82 lines (64 loc) · 2.79 KB
title COL_LENGTH (Transact-SQL)
description COL_LENGTH (Transact-SQL)
author markingmyname
ms.author maghan
ms.date 07/24/2017
ms.service sql
ms.subservice t-sql
ms.topic reference
ms.custom
ignite-2025
f1_keywords
COL_LENGTH
COL_LENGTH_TSQL
helpviewer_keywords
lengths [SQL Server], columns
COL_LENGTH function
column properties [SQL Server]
column length [SQL Server]
dev_langs
TSQL

COL_LENGTH (Transact-SQL)

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB]

This function returns the defined length of a column, in bytes.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

COL_LENGTH ( 'table' , 'column' )   

Arguments

' 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.

Return type

smallint

Exceptions

Returns NULL on error, or if a caller does not have the correct permission to view the object.

In [!INCLUDEssNoVersion], 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.

Remarks

For varchar columns declared with the max specifier (varchar(max)), COL_LENGTH returns the value -1.

Examples

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;  

[!INCLUDEssResult]

VarChar     NVarChar  
40          80  

See also

Expressions (Transact-SQL)
Metadata Functions (Transact-SQL)
COL_NAME (Transact-SQL)
COLUMNPROPERTY (Transact-SQL)