w3resource

Oracle LENGTH function

Description

The Oracle LENGTH function is used to return the length of a given string. If the string has data type CHAR, then the length includes all trailing blanks. If a string is null, then this function returns null.

The variations LENGTHB, LENGTHC, LENGTH2, and LENGTH4 use different units such as bytes or Unicode code points. LENGTHB uses bytes instead of characters. LENGTHC uses Unicode complete characters. LENGTH2 uses UCS2 code points. LENGTH4 uses UCS4 code points.

Uses of Oracle LENGTH Function
  • Determine String Length: Calculate the number of characters in a string.

  • Handle Trailing Blanks: Account for trailing spaces in CHAR data types.

  • Manage Null Values: Return null for null input strings.

  • Byte Length Calculation: Use LENGTHB to get the length in bytes for multi-byte character sets.

  • Unicode Character Length: Use LENGTHC for Unicode complete characters.

  • UCS2 Code Points: Use LENGTH2 to get the length in UCS2 code points.

  • UCS4 Code Points: Use LENGTH4 to get the length in UCS4 code points.

  • String Comparison: Compare the lengths of different strings for validation or processing.

Syntax:

{ LENGTH
| LENGTHB
| LENGTHC
| LENGTH2
| LENGTH4
}
(string1)

Parameters:

Name Description Data Types
string1 The string to return the length for. CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB exceptions are LENGTHC, LENGTH2, and LENGTH4, which do not allow char to be a CLOB 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('w3resource.com') "Length in characters"
FROM DUAL;
 

Sample Output:

Length in characters
--------------------
                  14

Previous: INSTR
Next: REGEXP_COUNT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/oracle/character-functions/oracle-length-function.php