Name

BIT_LENGTH, CHAR_LENGTH, and OCTET_LENGTH

Synopsis

All platforms stray from the ANSI standard in their support for the scalar functions for determining the length of expressions. While the platform support is nonstandard, the equivalent functionality exists under different names.

SQL2003 Syntax

The SQL2003 scalar functions for getting the length of a value take an expression to calculate the value and return the length as an integer. The BIT_LENGTH function returns the number of bits contained within the value of expression. The CHAR_LENGTH is the number of characters in the string expression. OCTET_LENGTH returns the number of octets within the string expression. All three of these functions will return NULL if expression is NULL.

BIT_LENGTH( expression )
CHAR_LENGTH( expression )
OCTET_LENGTH( expression )

DB2

DB2 does not support BIT_LENGTH, CHAR_LENGTH, or OCTET_LENGTH. DB2 does support a scalar function named LENGTH( ), which provides similar functionality for nongraphic string types. For graphic string types, LENGTH returns the number of double-byte characters in a string.

MySQL

MySQL supports CHAR_LENGTH and the SQL2003 synonym CHARACTER_LENGTH( ).

Oracle

The closest any of the platforms get to the BIT_LENGTH function is Oracle. Oracle supports the LENGTHB function, which returns an integer value representing the number of bytes in an expression. For the length of an expression in characters, Oracle provides a LENGTH( ) function as a synonym for CHAR_LENGTH.

PostgreSQL

PostgreSQL ...

Get SQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.