データ型の変換

多くの場合、あるデータ型の値を別のデータ型に変換できます。たとえば、 INTEGER の値は、 浮動小数点データ型 の値に変換できます。データ型の変換は、 キャスト と呼ばれます。

このトピックの内容:

明示的キャストと暗黙的キャスト

ユーザーは、あるデータ型から別のデータ型に値を明示的に変換できます。これは 明示的キャスト と呼ばれます。

状況によっては、Snowflakeは値を別のデータ型に自動的に変換します。これは、 暗黙的キャスト または 強制 と呼ばれます。

明示的キャスト

ユーザーは、次のオプションのいずれかを使用して、明示的に値をキャストできます。

  • CAST 関数。

  • The :: operator, called the cast operator.

  • The appropriate SQL function; for example, TO_DOUBLE.

たとえば、各クエリは文字列の値を DATE 値にキャストします。

SELECT CAST('2022-04-01' AS DATE);

SELECT '2022-04-01'::DATE;

SELECT TO_DATE('2022-04-01');
Copy

キャストは、 WHERE 句を含む、一般的な式が許可されているほとんどのコンテキストで許可されています。例:

SELECT date_column
  FROM log_table
  WHERE date_column >= '2022-04-01'::DATE;
Copy

暗黙的キャスト(強制)

強制は、関数(または演算子)が引数(またはオペランド)とは異なるが、互換性のあるデータ型を必要とする場合に発生します。

  • 関数またはストアドプロシージャの例:

    • 次のコードは、列 my_integer_column の INTEGER 値を FLOAT に強制して、 FLOAT を期待する関数 my_float_function() に値を渡すことができるようにします。

      SELECT my_float_function(my_integer_column)
        FROM my_table;
      
      Copy
  • 演算子の例:

    • 次のコードは、 INTEGER 値 17 を VARCHAR に強制して、 || 演算子を使用して値を連結できるようにします。

      SELECT 17 || '76';
      
      Copy

      この SELECT ステートメントの結果は、文字列 '1776' です。

    • The following statement coerces the INTEGER value in column my_integer_column to FLOAT so that the value can be compared to the value my_float_column by using the < comparison operator:

      SELECT ...
        FROM my_table
        WHERE my_integer_column < my_float_column;
      
      Copy

Not all contexts --- for example, not all operators --- support coercion.

キャスティングと優先順位

式内でキャストする場合、コードは、式内の他の演算子に対するキャスト演算子の優先順位を考慮に入れる必要があります。

次の例を考えてみましょう:

SELECT height * width::VARCHAR || ' square meters'
  FROM dimensions;
Copy

The cast operator has higher precedence than the arithmetic operator * (multiply), so the statement is interpreted as shown in the following example:

... height * (width::VARCHAR) ...
Copy

To cast the result of the expression height * width, use parentheses, as shown in the following example:

SELECT (height * width)::VARCHAR || ' square meters'
  FROM dimensions;
Copy

別の例として、次のステートメントを考えます。

SELECT -0.0::FLOAT::BOOLEAN;
Copy

You might expect this to be interpreted as shown in the following example:

SELECT (-0.0::FLOAT)::BOOLEAN;
Copy

したがって、 FALSE (0= FALSE、1= TRUE)を返すと予想されます。

However, the cast operator has higher precedence than the unary minus (negation) operator, so the statement is interpreted as shown in the following example:

SELECT -(0.0::FLOAT::BOOLEAN);
Copy

したがって、クエリ結果はエラーメッセージとなります。これは、単項マイナスを BOOLEAN に適用できないためです。

キャストできるデータ型

次のテーブルに、Snowflakeで有効なデータ型変換を示します。このテーブルには、Snowflakeが自動的に実行できる強制も示されています。

注釈

Internally, the CAST function and the :: operator call the appropriate conversion function. For example, if you cast a NUMBER to a BOOLEAN, Snowflake calls the TO_BOOLEAN function. The usage notes for each conversion function apply when the function is called indirectly by using a cast, and also when the function is called directly. For example, if you execute CAST(my_decimal_column AS BOOLEAN), the rules for calling TO_BOOLEAN with a DECIMAL value apply. For convenience, the table includes links to the relevant conversion functions.

For more information about conversions between semi-structured types and structured types, see 構造化型と半構造化型の変換.

ソースデータ型

ターゲットデータ型

キャスト可能

強制可能

変換関数

注意

ARRAY

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

VECTOR

変換には 明示的キャスト を使用します。詳細については、 ベクトル変換 をご参照ください。

BINARY

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

BOOLEAN

DECFLOAT

TO_DECFLOAT

For example, from FALSE to 0.

NUMBER

TO_NUMBER

None.

VARCHAR

TO_VARCHAR

たとえば、 TRUE から 'true' に。

VARIANT

TO_VARIANT

None.

DATE

TIMESTAMP

TO_TIMESTAMP

None.

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

DECFLOAT . (decimal floating-point numbers)

BOOLEAN

TO_BOOLEAN

たとえば、 0 から FALSE に。

FLOAT

TO_DOUBLE

None.

NUMBER[(p,s)]

TO_NUMBER

None.

VARCHAR

TO_VARCHAR

None.

FLOAT . (浮動小数点数)

BOOLEAN

TO_BOOLEAN

たとえば、 0.0 から FALSE に。

DECFLOAT

TO_DECFLOAT

None.

NUMBER[(p,s)]

TO_NUMBER

None.

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

GEOGRAPHY

VARIANT

TO_VARIANT

None.

GEOMETRY

VARIANT

TO_VARIANT

None.

NUMBER[(p,s)] . (INTEGERを含む固定小数点数)

BOOLEAN

TO_BOOLEAN

たとえば、 0 から FALSE に。

DECFLOAT

TO_DECFLOAT

None.

FLOAT

TO_DOUBLE

None.

TIMESTAMP

TO_TIMESTAMP

[1]

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

OBJECT

ARRAY

TO_ARRAY

None.

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

TIME

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

TIMESTAMP

DATE

TO_DATE , DATE

None.

TIME

TO_TIME , TIME

None.

VARCHAR

TO_VARCHAR

None.

VARIANT

TO_VARIANT

None.

VARCHAR

BOOLEAN

TO_BOOLEAN

たとえば、 'false' から FALSE に。

DATE

TO_DATE , DATE

None.

DECFLOAT

TO_DECFLOAT

None.

FLOAT

TO_DOUBLE

たとえば、 '12.34' から 12.34 に。

NUMBER[(p,s)]

TO_NUMBER

たとえば、 '12.34' から 12.34 に。

TIME

TO_TIME , TIME

None.

TIMESTAMP

TO_TIMESTAMP

None.

VARIANT

TO_VARIANT

None.

VARIANT

ARRAY

TO_ARRAY

None.

BOOLEAN

TO_BOOLEAN

たとえば、 'false' を含む VARIANT から FALSE に。

DATE

TO_DATE , DATE

None.

FLOAT

TO_DOUBLE

None.

GEOGRAPHY

TO_GEOGRAPHY

None.

NUMBER[(p,s)]

TO_NUMBER

None.

OBJECT

TO_OBJECT

None.

TIME

TO_TIME , TIME

None.

TIMESTAMP

TO_TIMESTAMP

None.

VARCHAR

TO_VARCHAR

None.

VECTOR

VARIANT には、 FLOAT または INT 型の ARRAY が含まれている必要があります。

VECTOR

ARRAY

TO_ARRAY

None.

注釈

For each listed data type --- for example, FLOAT --- the rules apply to all aliases for that data type. For example, the rules for FLOAT apply to DOUBLE, which is an alias for FLOAT.

使用上の注意

特に明記されていない限り、明示的なキャストと暗黙的なキャストの両方に次のルールが適用されます。

  • Conversion depends not only on the data type, but also the value, of the source; for example:

    • VARCHAR 値 '123' は数値に変換できますが、 VARCHAR 値 'xyz' は数値に変換できません。

    • The ability to cast a specific value of type VARIANT depends on the type of the data inside the VARIANT. For example, if the VARIANT contains a value of type TIME, then you can't cast the VARIANT value to a TIMESTAMP value, because you can't cast a TIME value to a TIMESTAMP value.

  • Snowflake performs implicit conversion of arguments to make them compatible. For example, if one of the input expressions is a numeric type, the return type is also a numeric type. That is, SELECT COALESCE('17', 1); first converts the VARCHAR value '17' to the NUMBER value 17, and then returns the first non-NULL value.

    When conversion isn't possible, implicit conversion fails. For example, SELECT COALESCE('foo', 1); returns an error because the VARCHAR value 'foo' can't be converted to a NUMBER value.

    We recommend passing in arguments of the same type or explicitly converting arguments if needed.

  • When implicit conversion converts a non-numeric value to a numeric value, the result is a value of type NUMBER(18,5).

    定数ではない数値文字列引数で、 NUMBER(18,5)が数値を表すのに十分でない場合は、値を表すことができる型に引数を キャスト します。

  • For some pairs of data types, conversion can result in loss of precision; for example:

    • FLOAT 値を INTEGER 値に変換すると、値が丸められます。

    • Converting a value from fixed-point numeric --- for example, NUMBER(38, 0) --- to floating point --- for example, FLOAT --- can result in rounding or truncation if the fixed-point number can't be precisely represented in a floating point number.

    • TIMESTAMP 値を DATE 値に変換すると、時刻に関する情報が削除されます。

  • Although Snowflake converts values in some situations where loss of precision can occur, Snowflake doesn't allow conversion in other situations where a loss of precision would occur. For example, Snowflake doesn't allow conversion when conversion would cause the following situations to happen:

    • VARCHAR 値の切り捨て。たとえば、Snowflakeは VARCHAR(10)を VARCHAR(5)には暗黙的にも明示的にもキャストしません。

    • Result in the loss of digits other than the least significant digits. For example, the following loss of digits fails:

      SELECT 12.3::FLOAT::NUMBER(3,2);
      
      Copy

      この例では、数値 12.3 には小数点の前に2桁ありますが、データ型 NUMBER(3,2) には小数点の前に1桁しかありません。

  • 精度の低いタイプから精度の高いタイプに変換する場合、変換ではデフォルト値が使用されます。たとえば、 DATE 値を TIMESTAMP_NTZ 値に変換すると、時、分、秒、端数秒が 0 に設定されます。

  • FLOAT 値が VARCHAR 値にキャストされる場合、後続のゼロは省略されます。

    For example, the following statements create a table and insert a row that contains a VARCHAR value, a FLOAT value, and a VARIANT value. The VARIANT value is constructed from JSON that contains a floating-point value represented with trailing zeros:

    CREATE OR REPLACE TABLE convert_test_zeros (
      varchar1 VARCHAR,
      float1 FLOAT,
      variant1 VARIANT);
    
    INSERT INTO convert_test_zeros SELECT
      '5.000',
      5.000,
      PARSE_JSON('{"Loan Number": 5.000}');
    
    Copy

    次の SELECT ステートメントは、 FLOAT 列と VARIANT 列内の FLOAT 値の両方を VARCHAR に明示的にキャストします。いずれの場合も、 VARCHAR には後続のゼロが含まれていません。

    SELECT varchar1,
           float1::VARCHAR,
           variant1:"Loan Number"::VARCHAR
      FROM convert_test_zeros;
    
    Copy
    +----------+-----------------+---------------------------------+
    | VARCHAR1 | FLOAT1::VARCHAR | VARIANT1:"LOAN NUMBER"::VARCHAR |
    |----------+-----------------+---------------------------------|
    | 5.000    | 5               | 5                               |
    +----------+-----------------+---------------------------------+
    
  • Some operations can return different data types, depending on a conditional expression. For example, the following IFNULL calls return slightly different data types depending on the input values:

    SELECT SYSTEM$TYPEOF(IFNULL(12.3, 0)),
           SYSTEM$TYPEOF(IFNULL(NULL, 0));
    
    Copy
    +--------------------------------+--------------------------------+
    | SYSTEM$TYPEOF(IFNULL(12.3, 0)) | SYSTEM$TYPEOF(IFNULL(NULL, 0)) |
    |--------------------------------+--------------------------------|
    | NUMBER(3,1)[SB1]               | NUMBER(1,0)[SB1]               |
    +--------------------------------+--------------------------------+
    

    If the expression has more than one possible data type, Snowflake chooses the data type based on the actual result. For more information about precision and scale in calculations, see 算術演算のスケールと精度. If the query generates more than one result --- for example, multiple rows of results --- Snowflake chooses a data type that is capable of holding each of the individual results.

  • Some applications, such as SnowSQL, and some graphical user interfaces, such as the Classic Console, apply their own conversion and formatting rules when they display data. For example, SnowSQL displays BINARY values as a string that contains only hexadecimal digits; that string is generated by implicitly calling a conversion function. Therefore, the data that SnowSQL displays might not unambiguously indicate which data conversions that Snowflake coerced.