简体   繁体   English

Athena - String to Date 覆盖

[英]Athena - String to Date coversion

I want to convert below string to only date column.我想将下面的字符串转换为仅日期列。 the column has both time and date stored as string该列将时间和日期都存储为字符串

String 2021-01-01 12:43:58 ==> 2021-01-01字符串2021-01-01 12:43:58 ==> 2021-01-01

You achieve what you want with this query:您可以通过此查询实现您想要的:

SELECT
cast(date_parse(column, '%Y-%m-%d') as date)
FROM
table

You can use date_format with date_parse or cast as date to achieve this:您可以使用date_formatdate_parse或 cast as date 来实现这一点:

SELECT date_format(date_parse('2021-01-01 12:43:58', '%Y-%m-%d %h:%i:%s'),'%Y-%m-%d')

will give 2021-01-01会给2021-01-01

Just replace the timestamp with column name只需将时间戳替换为列名

SELECT date_format(date_parse(<timestamp_column>, '%Y-%m-%d %h:%i:%s'),'%Y-%m-%d')

Now using cast as shown below现在使用 cast 如下所示

SELECT cast(date_parse('2021-01-01 12:43:58', '%Y-%m-%d %h:%i:%s') as date) SELECT cast(date_parse('2021-01-01 12:43:58', '%Y-%m-%d %h:%i:%s') as date)

similarly replace timestamp with column name同样用列名替换时间戳

For what is worth, here's a solution without casting back and forth from string to date:值得一提的是,这是一个无需从字符串到日期来回转换的解决方案:

select substr(column,1,10) from mytable

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

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