简体   繁体   English

使用错误处理将 Integer 转换为 Snowflake 中的日期

[英]Convert Integer to Date in Snowflake using Error Handling

I have a requirement where integer value should be converted to date type in Snowflake.Initially I used following query to get the desired result:我有一个要求,其中 integer 值应转换为 Snowflake 中的日期类型。最初我使用以下查询来获得所需的结果:

SELECT TO_DATE(TO_varchar(19000000+1200034),'YYYYMMDD') 2019-07-09 SELECT TO_DATE(TO_varchar(19000000+1200034),'YYYYMMDD') 2019-07-09

Now when I used same query for the input - "20200034", I am getting following error:现在,当我对输入使用相同的查询 - “20200034”时,我收到以下错误:

select TO_DATE(TO_varchar(19000000+1200034),'YYYYMMDD') select TO_DATE(TO_varchar(19000000+1200034),'YYYYMMDD')

Can't parse '20200034' as date with format 'YYYYMMDD'无法将“20200034”解析为格式为“YYYYMMDD”的日期

"20200034" is actually coming from one of the columns in snowflake table. “20200034”实际上来自雪花表中的一列。 To resolve this issue I tried using "TRY_TO_DATE" function, but output of "TRY_TO_DATE" function is giving incorrect result.为了解决这个问题,我尝试使用“TRY_TO_DATE”function,但“TRY_TO_DATE”function 的 output 给出的结果不正确。 Please find details below:请在下面找到详细信息:

select TRY_TO_DATE(TO_varchar(19000000+1200034)) select TRY_TO_DATE(TO_varchar(19000000+1200034))

1970-08-22 1970-08-22

As per Snowflake documentation, error handling function does not support optional format argument supported by TO_DATE, DATE.根据 Snowflake 文档,错误处理 function 不支持 TO_DATE、DATE 支持的可选格式参数。 https://docs.snowflake.com/en/sql-reference/functions/try_to_date.html https://docs.snowflake.com/en/sql-reference/functions/try_to_date.html

You can set the DATE_INPUT_FORMAT for the session before calling the TRY_TO_DATE function.您可以在调用 TRY_TO_DATE function 之前为 session 设置 DATE_INPUT_FORMAT。

I suggest you contact Snowflake support and ask them to enable try_to_date with format string - it's available but needs to be enabled manually.我建议您联系 Snowflake 支持并要求他们使用格式字符串启用 try_to_date - 它可用但需要手动启用。

However you have to be aware that TRY_TO_DATE on '20200034' will be resolved to NULL.但是您必须注意,“20200034”上的 TRY_TO_DATE 将被解析为 NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM