PostgreSQL character_length() Function

The PostgreSQL character_length() function returns the number of characters in a string. It is a multibyte safe function.

This function is the same as the character_length()char_length() function.

character_length() Syntax

This is the syntax of the PostgreSQL character_length() function:

character_length(string)

Parameters

string

Required. The string whose number of characters needs to count.

Return value

The PostgreSQL character_length() function is a multibyte-safe function that returns the number of characters in a string.

character_length() Examples

This example shows how to use the character_length() function to get the number of characters in a string.

SELECT
    character_length('a') AS "a",
    character_length('abc') AS "abc",
    character_length('Hello World') AS "Hello World";
 a | abc | Hello World
---+-----+-------------
 1 |   3 |          11

The PostgreSQL character_length() function is a multibyte-safe function. It also returns correctly if the string contains multibyte characters, such as Chinese.

SELECT character_length('你好') AS "character_length('你好')";
 character_length('你好')
--------------------------
                        2