MySQL CHAR_LENGTH() Function

The CHAR_LENGTH() function in MySQL determines the length of a string, counting the number of characters. It's a fundamental string function for various text processing tasks.



CHAR_LENGTH(): Definition and Usage

CHAR_LENGTH() is straightforward: it takes a string as input and returns its length as an integer. This is often used to validate data, to control string formatting, or to work with strings based on their size. It's functionally identical to CHARACTER_LENGTH().

Syntax

Syntax

CHAR_LENGTH(string)
      

Parameter Values

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

Examples

Finding the Length of a String Literal

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

Syntax

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

12
      

Finding the Length of Strings in a Column

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

Syntax

SELECT CHAR_LENGTH(CustomerName) AS LengthOfName
FROM Customers;
      
Output

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

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