MariaDB CHARSET() Function

In MariaDB, CHARSET() is a built-in function that returns the character set of a given string.

Please refer to the complete list of character sets supported by MariaDB .

MariaDB CHARSET() Syntax

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

CHARSET(str)

Parameters

str

Required. a string.

Return value

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

If the argument is NULL or is not a string, the MariaDB CHARSET() function will return binary.

MariaDB CHARSET() Examples

Example 1

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

SELECT CHARSET('hello');

Output:

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

Example 2

Let’s look at an example:

SELECT CHARSET(CONVERT('hello' USING latin1));

Output:

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

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

Example 3 - NULL

If the argument is NULL, the CHARSET() function will return binary.

SELECT CHARSET(NULL);

Output:

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

Example 4 - non-string

If the argument is not a string, the CHARSET() function will return binary.

SELECT CHARSET(159);

Output:

+--------------+
| CHARSET(159) |
+--------------+
| binary       |
+--------------+

Conclusion

In MariaDB, CHARSET() is a built-in function that returns the character set of a given string.