All Products
Search
Document Center

MaxCompute:FROM_CHARSET

Last Updated:Apr 01, 2024

Converts binary data that is in the specified encoding format into a UTF-8 encoded string for subsequent calculation. This topic describes the syntax and parameters of the FROM_CHARSET function and provides examples on how to use this function.

Syntax

STRING FROM_CHARSET(binary <source>, string <source_charset>, [string <mode>])

Parameters

  • source: required. A value of the BINARY type. This parameter specifies the binary data that you want to convert.

  • source_charset: required. A value of the STRING type. This parameter specifies the original encoding format of the binary data that is specified by source. Valid values: UTF-8, UTF-16, UTF-16LE, UTF-16BE, ISO-8859-1, US-ASCII, GB2312, GBK, and GB18030.

  • mode: optional. A value of the STRING type. This parameter specifies the processing mode if a character cannot be converted when the FROM_CHARSET function converts the binary data specified by source into a string in the specified encoding format. Valid values:

    • NONE: reports an error. No processing is performed. This is the default value.

    • TRANSLIT: replaces the character with a similar character in the specified encoding format.

    • IGNORE: ignores the error and continues to run the command.

Return value

A value of the STRING type in the UTF-8 encoding format is returned. If an input parameter is null or an empty string, the return value varies based on the following rules:

  • If an input parameter is null, null is returned.

  • If an input parameter is an empty string, an error is returned.

Examples

  • Example 1: Convert UTF-8 encoded binary data into a UTF-8 encoded string.

    SELECT FROM_CHARSET(unhex('e58aa0e6b2b9e9949fe696a4e68bb70a'),'UTF-8', 'TRANSLIT');

    The following result is returned:

    +------------+
    | _c0        |
    +------------+
    | 加油锟斤拷
         |
    +------------+
  • Example 2: Convert GBK-encoded binary data into a UTF-8 encoded string.

    SELECT FROM_CHARSET(unhex('b9feb9febac3a4ce'), 'GBK');

    The following result is returned:

    +------------+
    | _c0        |
    +------------+
    | 哈哈好の       |
    +------------+
  • Example 3: If an input parameter is null, null is returned.

    SELECT FROM_CHARSET(unhex('b9feb9febac3a4ce'), null);

    The following result is returned:

    +------------+
    | _c0        |
    +------------+
    | NULL       |
    +------------+
Note

The UNHEX function returns a string that is represented by a hexadecimal string. For more information, see UNHEX.

References