CHAR_LENGTH

CHAR_LENGTH is a function in SQL that returns the character length of a specified string.

CHAR_LENGTH(str)

  • str: This is the string for which the character length will be returned. CHAR_LENGTH(str) function returns the length of the given string argument as it would be if converted to a string type. It counts the number of characters, not bytes, so it effectively returns the number of characters in a string, taking into account the multibyte characters.

Example

SELECT CHAR_LENGTH('SQL Documentation');

Output

11

Explanation

The CHAR_LENGTH() function in MySQL returns the length of a string, in this case ‘SQL Documentation’, as a numeric value. This example returns 11, because there are 11 characters in ‘SQL Documentation’.

CHAR_LENGTH(character_expression) RETURN INT

  • character_expression: This is a required parameter that represents the string for which the number of characters will be returned. This can be a collection of characters, a character literal, a variable name or a column of a table. The function counts the number of characters in this string and returns the count result as an integer.

Example

SELECT CHAR_LENGTH('PostgreSQL');

Output

9

Explanation

In the example, the CHAR_LENGTH function returns the length of the given string 'PostgreSQL', which is 9. In PostgreSQL, CHAR_LENGTH is used to find the length of a character string.

CHAR_LENGTH( string_expression )

  • string_expression: It is the expression being tested for length. This can be any data type that can be implicitly converted to varchar or nvarchar such as character strings, binary strings, text, ntext, or image. The CHAR_LENGTH function measures the number of characters in string_expression.

Example

SELECT CHAR_LENGTH('SQL Documentation') AS Length;

Output

Length
18

Explanation

In the provided SQL query, the CHAR_LENGTH function is used to count the number of characters in the string ‘SQL Documentation’, resulting in 18.

CHAR_LENGTH(character_expression)

  • character_expression: This parameter represents the string for which the CHAR_LENGTH function will determine and return the number of characters. It can either be a string literal or a column of data type CHAR, NCHAR, NCHAR VARYING, VARCHAR2, CLOB, or NCLOB.

Example

SELECT CHAR_LENGTH('Hello, World!') FROM dual;

Output

13

Explanation

In the provided SQL statement, the CHAR_LENGTH function is used to return the length of the string ‘Hello, World!’, which is 13 characters, including punctuation and spaces.

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