Every character expression has a character set and a collation.
Specify the character set and collation for a character expression
The default character set and collation of a character expression, such as the simple statement SELECT 'string', are defined by the character_set_connection and collation_connection variables.
You can use the following syntax of the COLLATE clause to specify the character set and collation for a character expression:
[_charset_name]'string' [COLLATE collation_name]
Here is an example:
obclient> SELECT _utf8mb4'abc' COLLATE utf8mb4_unicode_ci;
+------------------------------------------+
| _utf8mb4'abc' COLLATE utf8mb4_unicode_ci |
+------------------------------------------+
| abc |
+------------------------------------------+
1 row in set
When you specify the character set of a constant to gb18030_2022 in an SQL statement, if the constant after _gb18030_2022 is a hexadecimal number, the number is converted into the corresponding character in the gb18030_2022 character set. If the constant after _gb18030_2022 is a string, its character set is set to gb18030_2022.
obclient> SELECT _gb18030_2022 0xCDE5 AS c FROM DUAL;
+-----+
| c |
+-----+
| B |
+-----+
1 row in set
obclient> SELECT _gb18030_2022 'B' AS c FROM DUAL;
+-----+
| c |
+-----+
| B |
+-----+
1 row in set
Select the character set and collation for a character expression
You can select the character set and collation for a character expression by using the following methods in OceanBase Database:
If you specify both the
_charset_nameandCOLLATE collation_nameparameters, thecharset_namecharacter set andcollation_namecollation are used.If you specify only the
_charset_nameparameter, the specifiedcharset_namecharacter set and the associated default collation are used. To view the default collation for each character set, execute theSHOW CHARACTER SETstatement.If you specify only the
COLLATE collation_nameparameter, the character set specified forcharacter_set_connectionand the default collation specified forcollation collation_nameare used.collation_namemust be a collation supported by the default character set.If neither
CHARACTER SETnorCOLLATEis specified, the character set specified forcharacter_set_connectionand the default collation specified forcollation collation_nameare used.