Skip to main content

char_length

1. CHAR_LENGTH Function

The CHAR_LENGTH function in H2 database is used to calculate the number of characters in a given string.

2. Syntax

The syntax for the CHAR_LENGTH function in H2 database is as follows:

CHAR_LENGTH(str)

Arguments

  • str: The string for which the character length needs to be calculated. It should be a string or a column of string datatype.

Return

  • The CHAR_LENGTH function returns the number of characters in the given string.

3. Notes

  • The CHAR_LENGTH function counts the number of characters, including spaces, in the string.
  • If the input string is NULL, the CHAR_LENGTH function will return NULL.
  • It is important to note that the CHAR_LENGTH function returns the number of characters, not the number of bytes or the number of Unicode code points in the string.

4. Examples

Here are a few examples demonstrating the usage of the CHAR_LENGTH function in H2 database:

Example 1 - Calculating the character length of a string:

SELECT CHAR_LENGTH('Hello World') AS length;

Output:

length
-------
11

Example 2 - Calculating the character length of a column:

CREATE TABLE names (
id INT PRIMARY KEY,
name VARCHAR(50)
);

INSERT INTO names VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');

SELECT id, name, CHAR_LENGTH(name) AS length
FROM names;

Output:

id |  name   | length
---+---------+-------
1 | Alice | 5
2 | Bob | 3
3 | Charlie | 7
  • LENGTH - Calculate the number of bytes in a string (not characters)
  • CHARACTER_LENGTH - Calculate the number of Unicode code points in a string