다음을 통해 공유


CAST_INVALID_INPUT 오류 클래스

SQLSTATE: 22018

형식의 값 <expression> 은 형식이 잘못되었기 때문에 로 <targetType> 캐스팅할 수 <sourceType> 없습니다. 구문에 따라 값을 수정하거나 대상 형식을 변경합니다. 를 사용하여 try_cast 잘못된 형식의 입력을 허용하고 NULL을 대신 반환합니다. 필요한 경우 이 오류를 무시하려면 "false"로 설정합니다 <ansiConfig> .

매개 변수

  • expression: 캐스팅해야 하는 식입니다. targettype
  • sourceType: 의 데이터 형식입니다 expression.
  • targetType: 캐스트 작업의 대상 형식입니다.
  • ansiConfig: ANSI 모드를 변경하는 구성 설정입니다.

설명

expression 다음 이유 중 하나로 인해 을 로 캐스팅 targetType 할 수 없습니다.

  • expression 가 형식의 도메인에 비해 너무 큽습니다. 예를 들어 해당 도메인은 에서 -128TINYINT 만 범위가 지정되므로 숫자를 1000 로 캐스팅할 +127수 없습니다.
  • expression 에는 형식의 일부가 아닌 문자가 포함되어 있습니다. 예를 들어 a 숫자 형식으로 캐스팅할 수 없습니다.
  • expression 는 캐스트 작업이 구문 분석할 수 없는 방식으로 형식이 지정됩니다. 예를 들어 1.0 및 는 1e1 정수 숫자 형식으로 캐스팅할 수 없습니다.

캐스트가 명시적으로 지정되지 않았을 수 있지만 Azure Databricks에서 암시적으로 삽입되었을 수 있습니다.

이 오류와 함께 제공되는 컨텍스트 정보는 개체와 오류가 발생한 식을 격리합니다.

도메인 및 허용되는 리터럴 형식에 대한 정의는 의 tyopeName데이터 형식에 대한 정의를 참조하세요.

완화 방법

이 오류에 대한 완화는 원인에 따라 달라집니다.

  • value 지정된 의 도메인 및 형식을 준수해야 합니까 typeName?

    입력 생성 값을 확인하고 데이터 원본을 수정합니다.

  • 캐스트의 대상이 너무 좁습니까?

    예를 들어 에서 DATE 로 또는 로 이동하여 형식을 BIGINTTIMESTAMPINTDOUBLE확장합니다.

  • 형식 value 이 잘못된가요?

    다음을 사용하는 것이 좋습니다.

    이러한 함수는 지정할 수 있는 다양한 형식을 허용합니다.

    소수점이 있는 숫자 리터럴(예: 1.0 또는 과학적 표기법) 1e0을 캐스팅할 때는 먼저 또는 다음으로 정확한 숫자로 DECIMALDOUBLE 이중 캐스팅하는 것이 좋습니다.

  • 잘못된 값이 있는 데이터가 예상되고 NULL을 생성하여 허용되어야 하나요?

    식 사용을 변경하거나 try_cast(값 AS typeName)을 삽입합니다. 이 함수는 형식을 충족하는 없이 value 전달될 때 를 반환 NULL 합니다.

    식을 마지막 수단으로 변경할 수 없는 경우 를 사용하여 ansiConfigANSI 모드를 일시적으로 사용하지 않도록 설정할 수 있습니다.

-- A view with a cast and string literals outside the domain of the target type
> CREATE OR REPLACE TEMPORARY VIEW v(c1) AS SELECT CAST(a AS SMALLINT) FROM VALUES('100'), ('50000') AS t(a);
> SELECT c1 FROM v;
 [CAST_INVALID_INPUT] The value '50000' of the type "STRING" cannot be cast to "SMALLINT" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
 == SQL of VIEW v(line 1, position 8) ==
 SELECT CAST(a AS SMALLINT) FROM VALUES('100'), ('50000') A...
        ^^^^^^^^^^^^^^^^^^^

-- Widen the target type to match the domain of the input
> CREATE OR REPLACE TEMPORARY VIEW v(c1) AS SELECT cast(a AS INTEGER) FROM VALUES('100'), ('50000') AS t(a);
> SELECT c1 FROM v;
 100
 50000

-- The input data format does not match the target type
> SELECT cast(a AS INTEGER) FROM VALUES('1.0'), ('1e0') AS t(a);
 [CAST_INVALID_INPUT] The value '1.0' of the type "STRING" cannot be cast to "INT" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
 == SQL(line 1, position 8) ==
 SELECT cast(a AS INTEGER) FROM VALUES('1.0'), ('1e0') AS ...
        ^^^^^^^^^^^^^^^^^^

-- Adjust the target type to the match the format if the format is indicative of the domain.
> SELECT cast(a AS DOUBLE) FROM VALUES('1.0'), ('1e0') AS t(a);
 1.0
 1.0

-- ALternatively double cast to preserver the target type
> SELECT cast(cast(a AS DOUBLE) AS INTEGER) FROM VALUES('1.0'), ('1e0') AS t(a);
 1
 1

-- The format of the numeric input contains display artifacts
> SELECT cast(a AS DECIMAL(10, 3)) FROM VALUES('12,345.30-'), ('12+') AS t(a);
 [CAST_INVALID_INPUT] The value '12,345.30-' of the type "STRING" cannot be cast to "DECIMAL(10,3)" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
 == SQL(line 1, position 8) ==
 SELECT cast(a AS DECIMAL(10, 3)) FROM VALUES('$<123,45.30>'), ('...
        ^^^^^^^^^^^^^^^^^^^^^^^^^

-- Use to_number() to parse formatted values
> SELECT to_number(a, '9,999,999.999S') FROM VALUES('123,45.30-'), ('12+') AS t(a);
 -12345.300
 12.000

-- The format of a date input does not match the default format
> SELECT cast(geburtsdatum AS DATE) FROM VALUES('6.6.2000'), ('31.10.1970') AS t(geburtsdatum);
 [CAST_INVALID_INPUT] The value '6.6.2000' of the type "STRING" cannot be cast to "DATE" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
 == SQL(line 1, position 8) ==
 SELECT cast(geburtsdatum AS DATE) FROM VALUES('6.6.2000'), ('31.1...
        ^^^^^^^^^^^^^^^^^^^^^^^^^^

-- Use to_date to parse the correct input format for a date
> SELECT to_date(geburtsdatum, 'dd.MM.yyyy') FROM VALUES('6.6.2000'), ('31.10.1970') AS t(geburtsdatum);
  2000-06-06
  1970-10-31

-- The type resolution of Databricks did not derive a sufficiently wide type, failing an implicit cast
> SELECT 12 * monthly AS yearly FROM VALUES ('1200'), ('1520.56') AS t(monthly);
 [CAST_INVALID_INPUT] The value '1520.56' of the type "STRING" cannot be cast to "BIGINT" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
 == SQL(line 1, position 8) ==
 SELECT 12 * monthly AS yearly FROM VALUES ('1200'),...
        ^^^^^^^^^^^^

-- Explicitly declare the expected type
> SELECT 12 * cast(monthly AS DECIMAL(8, 2)) AS yearly FROM VALUES ('1200'), ('1520.56') AS t(monthly);
 14400.00
 18246.72

-- The input data is occasionally expected to incorrect
> SELECT cast(salary AS DECIMAL(9, 2)) FROM VALUES('30000'), ('prefer not to say') AS t(salary);
 [CAST_INVALID_INPUT] The value 'prefer not to say' of the type "STRING" cannot be cast to "DECIMAL(9,2)" because it is malformed.
 Correct the value as per the syntax, or change its target type. Use `try_cast` to tolerate malformed input and return NULL instead.
 If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
== SQL(line 1, position 8) ==
SELECT cast(salary AS DECIMAL(9, 2)) FROM VALUES('30000'), ('prefer ...
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

-- Use try_cast to tolerate incorrect input
> SELECT try_cast(salary AS DECIMAL(9, 2)) FROM VALUES('30000'), ('prefer not to say') AS t(salary);
 30000.00
 NULL

-- In Databricks SQL temporarily disable ANSI mode to tolerate incorrect input.
> SET ANSI_MODE = false;
> SELECT cast(salary AS DECIMAL(9, 2)) FROM VALUES('30000'), ('prefer not to say') AS t(salary);
 30000.00
 NULL
> SET ANSI_MODE = true;

-- In Databricks Runtime temporarily disable ANSI mode to tolerate incorrect input.
> SET spark.sql.ansi.enabled = false;
> SELECT cast(salary AS DECIMAL(9, 2)) FROM VALUES('30000'), ('prefer not to say') AS t(salary);
 30000.00
 NULL
> SET spark.sql.ansi.enabled = true;