MySQL CHARACTER_LENGTH() Function

The CHARACTER_LENGTH() function in MySQL determines the length of a string by counting the number of characters it contains. This is a very useful function for string manipulation and data validation.



CHARACTER_LENGTH(): Definition and Usage

CHARACTER_LENGTH() is a simple yet powerful tool for working with text data. It helps to determine the size of strings, which is often needed for things like data validation, formatting, or extracting substrings. Note that CHARACTER_LENGTH() is equivalent to the CHAR_LENGTH() function.

Syntax

Syntax

CHARACTER_LENGTH(string)
      

Parameter Values

Parameter Description
string The string whose length you want to determine. This is required.

Examples

Finding the Length of a String

This example finds the length of the string "SQL Tutorial".

Syntax

SELECT CHARACTER_LENGTH("SQL Tutorial") AS LengthOfString;
      
Output

12
      

Finding the Length of Strings in a Column

This example retrieves the length of the 'CustomerName' for each customer in the 'Customers' table (assuming a table named 'Customers' exists with a 'CustomerName' column).

Syntax

SELECT CHARACTER_LENGTH(CustomerName) AS LengthOfName
FROM Customers;
      
Output

LengthOfName
-------------
(The length of each CustomerName from the Customers table)
      

**Note:** The second example's output will vary based on the data in your `Customers` table. Each row will show the length of the corresponding `CustomerName`.