IS_ENCODING

Updated at:

This function checks if the input string str can be converted from a source character set from_encoding to a target character set to_encoding without generating garbled text. A common use case is checking conversions where from_encoding is UTF-8 and to_encoding is GBK.

Syntax

boolean is_encoding(string <str>, string <from_encoding>, string <to_encoding>)

Parameters

  • str: Required. string. The input string. An empty string is considered valid for any character set.

  • from_encoding, to_encoding: Required. string. The source and target character sets.

Return value

Returns a boolean value:

  • Returns true if str is successfully converted, or false otherwise.

  • Returns NULL if from_encoding or to_encoding is NULL.

Examples

  • Example 1: Determine whether the characters 测试 or 測試 can be converted from the utf-8 character set to the gbk character set. The command is as follows.

    --Returns true.
    select is_encoding('测试', 'utf-8', 'gbk');
    --Returns true.
    select is_encoding('測試', 'utf-8', 'gbk');
  • Example 2: Determine whether the characters 測試 can be converted from the utf-8 character set to the gb2312 character set. The following is a sample command.

    --Returns false because the GB2312 character set does not include the traditional Chinese characters in '測試'.
    select is_encoding('測試', 'utf-8', 'gb2312');
  • Example 3: Pass NULL as an input parameter.

    --Returns NULL.
    select is_encoding('测试', null, 'gbk');

Related functions

IS_ENCODING is a string function. For more string functions, see String functions.