简体   繁体   English

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

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

I currently have tables with dates, set up as VARCHAR in the format of YYYY-MM-DD such as:我目前有带日期的表,以 YYYY-MM-DD 格式设置为 VARCHAR,例如:

2017-01-01

The date column I'm working with is called 'event_dt'我正在使用的日期列称为“event_dt”

I'm used to being able to use day(event_dt), month(event_dt), year(event_dt) etc. in Hive, but Presto just gives me error executing query with no other explanation when the queries fail.我习惯于在 Hive 中使用day(event_dt), month(event_dt), year(event_dt)等,但是当查询失败时,Presto 只是给我error executing query而没有其他解释。

So, for example, I've tried:因此,例如,我尝试过:

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

I would expect the output to read:我希望 output 能够读取:

01

but all I get is [Code: 0, SQL State: ] Error executing query但我得到的只是[Code: 0, SQL State: ] Error executing query

I've tried a few other methods listed in the Presto documentation but am having no luck at all.我尝试了 Presto 文档中列出的其他一些方法,但完全没有运气。 I realize this is probably very simple but any help would be much appreciated.我意识到这可能非常简单,但任何帮助将不胜感激。

You can use themonth() function after converting the varchar to a date with the date() function:在使用 date() function 将varchar转换为date后,您可以使用month() date() function:

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

Thanks to @LukStorms in the comments to the original question, I've found two solutions:感谢@LukStorms 在对原始问题的评论中,我找到了两个解决方案:

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

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

相关问题 如何从日期中提取日、月和年? - How to extract the Day, Month and Year from the date? 从日期字段mysql中提取年和月 - extract year and month from date field mysql 如何在 Presto SQL 中将 unix 时间戳转换为日期并从中提取月份 - How to cast unix timestamp as date and extract month from it in Presto SQL 如何从具有varchar数据类型的列中获取日期,月份和年份? - How to fetch date, month and year from column with varchar data type? 如何使用 Sql/Presto 从数字字符串中提取年份? - How to extract the year from a numeric string using Sql/Presto? 如何从 Presto ARRAY(MAP(VARCHAR, VARCHAR)) 中提取元素 - How to extract elements from Presto ARRAY(MAP(VARCHAR, VARCHAR)) 如何在不使用子查询的情况下从oracle函数中提取年或月,日? - How to extract year or month,day from oracle function without using sub-query? 如何比较日期(年,月)和日期(日,月,年) - How to compare date(year, month) with date(day, month, year) 从日期列中获取最后 N 个周期,而不考虑周期(年、月、日、小时等) - Get the last N periods from a date column, regardless the period (year, month, day, hour, etc.) 如何从格式化的列日期中获取特定的日期月份或年份 - how to get specific day month or year from a column date formated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM