MySQL CHARSET() Function

MySQL CHARSET() function returns the character set of the specified string.

CHARSET() Syntax

Here is the syntax of the MySQL CHARSET() function:

CHARSET(str)

Parameters

str

Required. The string.

Return value

The CHARSET() function returns the character set of the specified string.

The CHARSET() function will return NULL if the argument is binary.

CHARSET() Examples

The following example shows how to use the CHARSET() function to get the character set of a string parameter.

SELECT CHARSET('hello');
+------------------+
| CHARSET('hello') |
+------------------+
| utf8mb4          |
+------------------+

Let’s look at an example:

SELECT CHARSET(CONVERT('hello' USING latin1));
+----------------------------------------+
| CHARSET(CONVERT('hello' USING latin1)) |
+----------------------------------------+
| latin1                                 |
+----------------------------------------+

Here, we used the CONVERT() function to convert the character set of the string 'hello' to latin1 first, and then use the CHARSET() function to get the character set of the converted string.

The CHARSET() function will return NULL if the argument is binary.

SELECT CHARSET(NULL);
+---------------+
| CHARSET(NULL) |
+---------------+
| binary        |
+---------------+