Returns the number of characters in an expression.
char_length(char_expr | uchar_expr)
is a character-type column name, variable, or constant expression of char, varchar, nchar or nvarchar type.
is a character-type column name, variable, or constant expression of unichar or univarchar type.
select char_length(notes) from titles where title_id = "PC9999"
----------- 39
declare @var1 varchar(20), @var2 varchar(20), @char char(20) select @var1 = "abcd", @var2 = "abcd ", @char = "abcd" select char_length(@var1), char_length(@var2), char_length(@char)
----------- ----------- ----------- 4 8 20
char_length, a string function, returns an integer representing the number of characters in a character expression or text value.
For variable-length columns and variables, char_length returns the number of characters (not the defined length of the column or variable). If explicit trailing blanks are included in variable-length variables, they are not stripped. For literals and fixed-length character columns and variables, char_length does not strip the expression of trailing blanks (see Example 2).
For multi-byte character sets, the number of characters in the expression is usually less than the number of bytes; use datalength to determine the number of bytes.
For Unicode expressions, returns the number of Unicode values (not bytes) in an expression. Surrogate pairs count as two Unicode values.
If char_expr or uchar_expr is NULL, char_length returns NULL.
For general information about string functions, see “String functions”.
SQL92 – Compliance level: Transact-SQL extension.
Any user can execute char_length.
Function datalength