To determine the length of a given string, use the Oracle LENGTH() function. The length includes all trailing blanks if the string's data type is CHAR. This function returns null when a string is empty.
Bytes, not characters, are what LENGTHB
counts. LENGTHC
uses Unicode complete characters. Codepoints in the Unicode Character Set 2 (UCS2) are used by LENGTH2
. LENGTH4
uses UCS4 code points.
Syntax:
{ LENGTH | LENGTHB | LENGTHC | LENGTH2 | LENGTH4 } (string1)
string1
can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The return value is of datatype NUMBER. If Any of the following data types may be used for string1: CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The type of the returned value is NUMBER. If char's datatype is CHAR, then the length reports all trailing blanks. This function will return an empty string if string1 is null.
Arguments:
Name | Description | Data Types |
---|---|---|
string1 | The string to return the length for. | LENGTHC, LENGTH2, and LENGTH4 are the only exceptions that allow char to be a CLOB or NCLOB instead of a CHAR, VARCHAR2, NCHAR, NVARCHAR2, or NCLOB. |
Return Value Type
NUMBER
Applies to
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
Examples: Oracle LENGTH function
The following example uses the LENGTH function using a single-byte database character set:
SELECT LENGTH('Oracle database') "Length" FROM DUAL;
Sample Output:
15
Leave a comment