簡體   English   中英

如何使用 Presto 從 varchar 日期字段中提取日/月/年等?

[英]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'

我希望 output 能夠讀取:

01

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

我嘗試了 Presto 文檔中列出的其他一些方法,但完全沒有運氣。 我意識到這可能非常簡單,但任何幫助將不勝感激。

在使用 date() function 將varchar轉換為date后,您可以使用month() date() function:

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

感謝@LukStorms 在對原始問題的評論中,我找到了兩個解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM