CHARACTER_LENGTH
CHARACTER_LENGTH function in SQL returns the length of a string, measured in characters. It considers spaces, special characters, and regular alphanumeric characters as part of the string length.
CHARACTER_LENGTH(expression)
- expression: The string argument for which the length will be calculated. This can be a constant, variable, or column of either character or binary data type. The function will return the number of characters in the expression.
Example
SELECT CHARACTER_LENGTH('Hello World') AS Length;
Output
+--------+| Length |+--------+| 11 |+--------+
Explanation
The CHARACTER_LENGTH
string function in MySQL returns the length of the specified string character. In the provided example, it returns the length of the string ‘Hello World’ which is 11.
CHARACTER_LENGTH(text) RETURNS integer
- text: The input string for which the function will return the number of characters. The input can be of type `CHAR`, `VARCHAR`, or `TEXT`.
- returns integer: This indicates that the function will return an integer. Specifically, it returns the character length or number of characters in the specified string.
Example
SELECT CHARACTER_LENGTH('PostgreSQL');
Output
11
Explanation
The CHARACTER_LENGTH
function returns the number of characters in the given string ‘PostgreSQL’, in this case, it returns 11.
CHARACTER_LENGTH( string )
- string: This is the input parameter where a string of characters is entered. CHARACTER_LENGTH function will return the number of characters in this supplied string.
Example
SELECT CHARACTER_LENGTH('Hello world') AS Length;
Output
Length11
Explanation
The CHARACTER_LENGTH
function is a string function that returns the number of characters in a string. In this example, the function returned 11 as the length of the string ‘Hello world’.
CHARACTER_LENGTH(expression)
- expression: The string value or the column name of which the character length is to be calculated. Its data type must be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The function will return the length of the provided string.
Example
SELECT CHARACTER_LENGTH('Hello World') FROM dual;
Output
11
Explanation
In this example, the CHARACTER_LENGTH function was used to calculate the length of the string 'Hello World'
. The space in between Hello
and World
is considered one character, making the total length of the string 11 characters long.