[英]how to convert timestamps in a nullable column to dates in aws athena?
I want to convert datatype of string (eg: '2018-03-27T00:20:00.855556Z' ) into date (eg: '2018-03-27')我想将字符串的数据类型(例如:'2018-03-27T00:20:00.855556Z')转换为日期(例如:'2018-03-27')
problem is cast(substring(dt, 1, 10) as date) doesn't work here (INVALID_CAST_ARGUMENT: Value cannot be cast to date) and the column has null values and empty strings so from_iso8601_timestamp will fail (INVALID_FUNCTION_ARGUMENT: Invalid format: "")问题是 cast(substring(dt, 1, 10) as date) 在这里不起作用(INVALID_CAST_ARGUMENT:无法将值转换为日期)并且该列具有 null 个值和空字符串,因此 from_iso8601_timestamp 将失败(INVALID_FUNCTION_ARGUMENT:格式无效:“ ")
problem is cast(substring(dt, 1, 10) as date) doesn't work here
问题是 cast(substring(dt, 1, 10) as date) 在这里不起作用
Was not able to reproduce, works just fine for me -无法重现,对我来说效果很好-
select cast(substring('2018-03-27T00:20:00.855556Z', 1, 10) as date);
Output: Output:
_col0 ![]() |
---|
2018-03-27 ![]() |
The problem seems to be the same as for from_iso8601_timestamp
- empty strings, so you can usetry
(swallows all exceptions and returns null):问题似乎与
from_iso8601_timestamp
相同 - 空字符串,因此您可以使用try
(吞下所有异常并返回 null):
select try(from_iso8601_timestamp(dt)) dt
from (values ('2018-03-27T00:20:00.855556Z'),(null), ('')) as t(dt);
or check if string is empty:或者检查字符串是否为空:
select from_iso8601_timestamp(if(dt = '', null, dt)) dt
from (values ('2018-03-27T00:20:00.855556Z'),(null), ('')) as t(dt);
Output: Output:
dt ![]() |
---|
2018-03-27 00:20:00.855 UTC ![]() |
NULL ![]() |
NULL ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.