如何使用 Presto 从 varchar 日期字段中提取 day/month/year 等?

How to extract day/month/year etc from varchar date field, using Presto?

我目前有日期表,设置为 YYYY-MM-DD 格式的 VARCHAR,例如:

2017-01-01

我正在使用的日期列称为 'event_dt'

我习惯于在 Hive 中使用 day(event_dt), month(event_dt), year(event_dt) 等,但是当查询失败时 Presto 只给我 error executing query 而没有其他解释。

所以,例如,我试过:

select
month(event_dt)
from
my_sql_table
where
event_dt = '2017-01-01'

我希望输出为:

01

但我得到的只是[Code: 0, SQL State: ] Error executing query

我已经尝试了 Presto 文档中列出的其他几种方法,但我一点运气都没有。我意识到这可能非常简单,但我们将不胜感激。

感谢@LukStorms 在对原始问题的评论中,我找到了两个解决方案:

  1. 使用月份(cast(event_dt as date))
  2. 使用月份(日期(event_dt))

您可以使用month() function after converting the varchar to a date with the date()函数:

presto> select month(date('2017-01-01'));
 _col0
-------
     1
(1 row)