Skip to main content

CONVERT_TO

Description​

Converts the character encoding of a column to a specified target character set. This function is typically used in the ORDER BY clause to ensure that columns containing Chinese characters are sorted according to their pinyin order. Currently, only conversion to 'gbk' is supported.

Syntax​

CONVERT_TO(<column>, <character>)

Parameters​

ParametersDescription
<column>The VARCHAR column whose encoding is to be converted.
<character>The target character set. Currently, only 'gbk' is supported.

Return Value​

Returns a VARCHAR value with the converted encoding, allowing proper pinyin-based ordering when used in the ORDER BY clause.

Examples​

SELECT * FROM class_test ORDER BY class_name;
+----------+------------+-------------+
| class_id | class_name | student_ids |
+----------+------------+-------------+
| 6 | asd | [6] |
| 7 | qwe | [7] |
| 8 | z | [8] |
| 2 | ε“ˆ | [2] |
| 3 | 哦 | [3] |
| 1 | ε•Š | [1] |
| 4 | εΌ  | [4] |
| 5 | ζˆ‘ | [5] |
+----------+------------+-------------+
SELECT * FROM class_test ORDER BY CONVERT_TO(class_name, 'gbk');
+----------+------------+-------------+
| class_id | class_name | student_ids |
+----------+------------+-------------+
| 6 | asd | [6] |
| 7 | qwe | [7] |
| 8 | z | [8] |
| 1 | ε•Š | [1] |
| 2 | ε“ˆ | [2] |
| 3 | 哦 | [3] |
| 5 | ζˆ‘ | [5] |
| 4 | εΌ  | [4] |
+----------+------------+-------------+