MariaDB COLLATION() Function

In MariaDB, COLLATION() is a built-in function that returns the collation for a given string.

Please refer to the complete list of collations supported by MariaDB .

MariaDB COLLATION() Syntax

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

COLLATION(str)

Parameters

str

Required. a string.

Return value

The MariaDB COLLATION() function returns the collation of the specified string.

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

MariaDB COLLATION() Examples

Example 1

The following example shows how to use the COLLATION() function to get the collation of a string parameter.

SELECT COLLATION('hello');

Output:

+--------------------+
| COLLATION('hello') |
+--------------------+
| utf8mb4_general_ci |
+--------------------+

Example 2

Let’s look at an example:

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

Output:

+------------------------------------------+
| COLLATION(CONVERT('hello' USING latin1)) |
+------------------------------------------+
| latin1_swedish_ci                        |
+------------------------------------------+

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

Example 3 - NULL

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

SELECT COLLATION(NULL);

Output:

+-----------------+
| COLLATION(NULL) |
+-----------------+
| binary          |
+-----------------+

Example 4 - non-string

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

SELECT COLLATION(159);

Output:

+----------------+
| COLLATION(159) |
+----------------+
| binary         |
+----------------+

Conclusion

In MariaDB, COLLATION() is a built-in function that returns the collation for a given string.