LENGTHB

LENGTHB is a function in SQL that returns the length of a string, measured in bytes. The function supports strings in multibyte character sets, providing the byte length of the string rather than the character count.

LENGTHB(character_expression)

  • character_expression: This parameter represents the string for which the length is to be calculated. It can be of data type CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, NCLOB, or a string literal. LENGTHB returns the length in bytes instead of characters. The value returned by LENGTHB is of NUMBER data type. This function does not count trailing blanks.

Example

SELECT LENGTHB('SHEEP') AS String_Length FROM dual;

Output

STRING_LENGTH
-------------
5

Explanation

The SQL code selects the byte length of the string ‘SHEEP’. The LENGTHB function in Oracle SQL returns the length in bytes of a given string. In this example, it returns 5, which is the number of bytes in ‘SHEEP’.

LENGTHB(string text) RETURNS int

  • string text: It represents the string for which the length is calculated. Length is calculated in bytes.

Example

SELECT LENGTHB('PostgreSQL');

Output

11

Explanation

The LENGTHB function in PostgreSQL returns the length of the specified string, in bytes. In the provided example, ‘PostgreSQL’ is 11 bytes long, so the output is 11.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.