CAST()
You may also use CAST()
to convert a string to a different character
set. The new format is:
CAST ( character_string AS character_data_type CHARACTER SET character_set_name )
Example:
SELECT CAST(_latin1'test' AS CHAR CHARACTER SET utf8);
You may not use a COLLATE
clause inside a CAST()
, but you may use
it outside, that is, CAST(... COLLATE ...)
is illegal but
CAST(...) COLLATE ...
is legal.
Example:
SELECT CAST(_latin1'test' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin;
If you use CAST()
without specifying CHARACTER SET
, then the
resulting character set and collation are the connection/literal
character set and its default collation. If you use CAST()
with
CHARACTER SET X
, then the resulting character set is X
and the
resulting collation is X
's default collation.