无法将变体值“”强制转换为TIMESTAMP_NTZ

时间:2020-05-25 16:04:21

标签: snowflake-cloud-data-platform

JSON样本数据

{"location": {"city": "Lexington","zip": "40503"},"dimensions": {"sq_ft": "1000"},"type": "Residential","sale_date": "2016-02-6","price": "75836"},
{"location": {"city": "Belmont","zip": "02478"},"dimensions": {"sq_ft": "1103"},"type": "Residential","sale_date": "2016-10-16","price": "92567"},
{"location": {"city": "Winchester","zip": "01890"},"dimensions": {"sq_ft": "1122"},"type": "Condo","sale_date": "2016-11-26","price": "89921"},
{"location": {"city": "Winchester","zip": "01890"},"dimensions": {"sq_ft": "1122"},"type": "Condo","sale_date": "","price": "89921"}

ddl

create or replace table sales_json_details(
    dimensions varchar(1000),
    location varchar(1000),
    price number,
    sale_date timestamp,
    type varchar(100)
 )

查询以加载数据

copy into sales_json_details from @internal_stage_json_demo 
    file_format = (type = json NULL_IF='' ) 
    MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE;

异常:

**Failed to cast variant value "" to TIMESTAMP_NTZ**

如何使用复制选项NULL加载""而不是空字符串MATCH_BY_COLUMN_NAME= CASE_INSENSITIVE

1 个答案:

答案 0 :(得分:0)

似乎在检查NULL_IF之前尝试转换为时间戳,因此您可以使用NULLIF函数:

create or replace file format JSONFORMAT type = JSON;

copy into sales_json_details from (
SELECT $1:dimensions::VARCHAR, 
$1:location::VARCHAR, 
$1:price::VARCHAR, 
NULLIF($1:sale_date::varchar,'' ), 
$1:type::VARCHAR
from  @internal_stage_json_demo ( FILE_FORMAT => JSONFORMAT));

它将按预期加载数据。

当我们转换列时,不需要MATCH_BY_COLUMN_NAME也不被支持。如果主要目的是使用MATCH_BY_COLUMN_NAME,则应将sale_date列定义为varchar:

create or replace table sales_json_details( dimensions varchar(1000), location varchar(1000), price number, sale_date varchar, type varchar(100) );

copy into sales_json_details from @internal_stage_json_demo 
file_format = (type = json NULL_IF='' ) 
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE;