LENGTHB

Updated at:
Copy as MD

Returns the byte length of a string. Unlike LENGTH, which counts characters, LENGTHB counts bytes — the two differ for strings that contain multi-byte UTF-8 characters such as Chinese, Japanese, or accented Latin characters.

Syntax

bigint lengthb(string <str>)

Parameters

Parameter Required Type Description
str Yes STRING The string to measure. If the input value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, it is implicitly converted to STRING before calculation.

Return value

Returns a BIGINT value representing the number of bytes in str.

  • If str is null, null is returned.

  • If str is not of the STRING, BIGINT, DOUBLE, DECIMAL, or DATETIME type, an error is returned.

Examples

Example 1: ASCII string

-- Returns 15
select lengthb('Tech on the net');

Example 2: Null input

-- Returns null
select lengthb(null);

Related functions

LENGTHB is a string function. For more string functions, see String functions.