char_length

Returns the number of characters in an expression.

Syntax

char_length(char_expr | uchar_expr)

Parameters

Examples

Usage

For:
  • Compressed large object (LOB) columns, char_length returns the number of original plain text characters.

  • 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).

  • unitext, unichar, and univarchar columns, char_length returns the number of Unicode values (16-bit), with one surrogate pair counted as two Unicode values. For example, this is what is returned if a unitext column ut contains row value U+0041U+0042U+d800dc00:
    select char_length(ut) from unitable
    ------------
    4
  • Multibyte character sets, the number of characters in the expression is usually fewer than the number of bytes; use datalength to determine the number of bytes.

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

See also Transact-SQL Users Guide.

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

Any user can execute char_length.

Related reference
datalength