Decodes binary data from a specified source encoding into a UTF-8 string. If any input parameter is null, null is returned.
Syntax
STRING FROM_CHARSET(binary <source>, string <source_charset>[, string <mode>])Parameters
source
Required. Type: BINARY. The binary data to decode.
source_charset
Required. Type: STRING. The source encoding of the binary data. Valid values: UTF-8, UTF-16, UTF-16LE, UTF-16BE, ISO-8859-1, US-ASCII, GB2312, GBK, GB18030.
mode
Optional. Type: STRING. The behavior when a character cannot be converted. If not specified, the function reports an error and stops execution. Valid values:
| Value | Description |
|---|---|
NONE | Reports an error and stops execution. Default value. |
TRANSLIT | Replaces the unconvertible character with a visually similar character. |
IGNORE | Skips the unconvertible character and continues execution. |
Return value
Returns a STRING value in UTF-8 encoding.
| Input condition | Return value |
|---|---|
| Any parameter is null | NULL |
| Any parameter is an empty string | Error |
Examples
Example 1: Decode UTF-8 encoded binary data with TRANSLIT mode
SELECT FROM_CHARSET(unhex('e58aa0e6b2b9e9949fe696a4e68bb70a'), 'UTF-8', 'TRANSLIT');Result:
+------------+
| _c0 |
+------------+
| 加油锟斤拷
|
+------------+Example 2: Decode GBK-encoded binary data
SELECT FROM_CHARSET(unhex('b9feb9febac3a4ce'), 'GBK');Result:
+------------+
| _c0 |
+------------+
| 哈哈好の |
+------------+Example 3: Null input returns null
SELECT FROM_CHARSET(unhex('b9feb9febac3a4ce'), null);Result:
+------------+
| _c0 |
+------------+
| NULL |
+------------+The UNHEX function returns a string that is represented by a hexadecimal string. For details, see UNHEX.
What's next
MaxCompute supports user-defined functions (UDFs) when built-in functions do not meet your requirements. See UDF overview.
For MaxCompute SQL FAQ and common errors, see: