すべてのプロダクト
Search
ドキュメントセンター

MaxCompute:DECODE

最終更新日:Jan 17, 2025

パラメーターに基づいてさまざまな機能を実装します。 DECODE関数を使用して、IF THEN ELSEの分岐選択を実装できます。 DECODE関数を使用して、charsetで指定された形式に基づいてstrで指定された文字列をデコードすることもできます。

IF THEN ELSEブランチ選択の実装

構文

decode(<expression>, <search>, <result>[, <search>, <result>]...[, <default>])

Parameters

  • expression: 必須です。 比較する式。The expression that you want to compare.

  • search: 必須です。 との比較に使用される検索項目。

  • result: 必須です。 searchの値がexpressionの値と一致した場合に返される値。

  • default: オプション。 式に一致する検索項目がない場合、defaultの値が返されます。 このパラメーターに値を指定しない場合は、nullが返されます。

説明
  • null値を除いて、resultパラメーターの他のすべての値は同じデータ型でなければなりません。 値が異なるデータ型の場合、エラーが返されます。

  • searchexpressionの値は同じデータ型である必要があります。 それ以外の場合は、エラーが返されます。

戻り値

  • 検索項目が式と一致する場合、戻り値はresultになります。

  • 式に一致する検索項目がない場合、defaultの値が返されます。

  • defaultパラメーターに値が指定されていない場合、nullが返されます。

  • 重複した検索項目が式と一致する場合、最初の検索項目の値が返されます。

  • ほとんどの場合、MaxCompute SQLがnull=NULLを計算すると、戻り値はNULLになります。 ただし、DECODE関数は、2つのnull値が同じであると見なします。

sale_detailテーブルには、shop_name文字列、customer_id文字列、total_price double、列が含まれます。 このテーブルには、次のデータが含まれます。

+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
| null       | c5          | NULL        | 2014       | shanghai   |
| s6         | c6          | 100.4       | 2014       | shanghai   |
| s7         | c7          | 100.5       | 2014       | shanghai   |
+------------+-------------+-------------+------------+------------+

サンプル文:

-- If the value of customer_id is c1, Taobao is returned. If the value is c2, Alipay is returned. If the value is c3, Aliyun is returned. If the value is null, N/A is returned. In other cases, Others is returned. 
select
decode(customer_id,
'c1', 'Taobao',
'c2', 'Alipay',
'c3', 'Aliyun',
Null, 'N/A',
'Others') as result
from sale_detail;
-- The preceding statement is equivalent to the following statement: 
if customer_id = c1 then
result := 'Taobao';
elsif customer_id = c2 then
result := 'Alipay';
elsif customer_id = c3 then
result := 'Aliyun';
...
else
result := 'Others';
end if;

次の応答が返されます。

+------------+
| result     |
+------------+
| Others     |
| Others     |
| Others     |
| Taobao     |
| Alipay     |
| Aliyun     |
+------------+

で指定された文字列をデコードします。strに基づく形式によって指定されたcharset

構文

string decode(binary <str>, string <charset>)

Parameters

  • str: required。 デコードする文字列。 文字列はBINARY型です。

  • charset: 必須です。 STRING型のエンコード形式。 有効な値: UTF-8、UTF-16、UTF-16LE、UTF-16BE、ISO-8859-1、US-ASCII。

    説明

    現在、ISO-8859-1およびUS-ASCII符号化フォーマットは、英字のみを符号化または復号化するために使用され得る。

戻り値

STRING型の値が返されます。 strまたはcharsetの値がnullの場合、戻り値はnullになります。

  • 例1: UTF-8形式に基づいて文字列をエンコードおよびデコードします。 サンプル文:

    -- Encode and decode the string.
    select decode(encode("中文样例", "UTF-8"), "UTF-8");
    -- The following result is returned:
    +-----+
    | _c0 |
    +-----+
    | 中文样例 |
    +-----+
  • 例2: 入力パラメーターをnullに設定します。 サンプル文:

    -- The return value is null. 
    select decode(encode("中文样例","UTF-8"), null);
    | _c0 |
    +-----+
    | NULL |
    +-----+

関連関数

DECODEは、他のビジネスシナリオで使用される関数として分類されます。 他のビジネスシナリオで使用される関数の詳細については、その他の関数.

DECODEも文字列関数です。 文字列検索と変換に関連する関数の詳細については、文字列関数.