【问题标题】:Presto SQL- convert date which is in string datatypes into date format yyyy/mm/ddPresto SQL-将字符串数据类型中的日期转换为日期格式yyyy/mm/dd
【发布时间】:2021-11-10 14:32:52
【问题描述】:

ds:yyyy/mm/dd 格式的日期。它以文本的形式存储,我们使用presto来运行。不需要日期功能

CREATE TABLE job_data(
    ds DATE,
    job_id INT NOT NULL,
    actor_id INT NOT NULL,
    event VARCHAR(15) NOT NULL,
    language VARCHAR(15) NOT NULL,
    time_spent INT NOT NULL,
    org CHAR(2) );

INSERT INTO job_data (ds, job_id, actor_id, event, language, time_spent, org)
VALUES ('2020-11-30', 21, 1001, 'skip', 'English', 15, 'A'),
    ('2020-11-30', 22, 1006, 'transfer', 'Arabic', 25, 'B'),
    ('2020-11-29', 23, 1003, 'decision', 'Persian', 20, 'C'),
    ('2020-11-28', 23, 1005,'transfer', 'Persian', 22, 'D'),
    ('2020-11-28', 25, 1002, 'decision', 'Hindi', 11, 'B'),
    ('2020-11-27', 11, 1007, 'decision', 'French', 104, 'D'),
    ('2020-11-26', 23, 1004, 'skip', 'Persian', 56, 'A'),
    ('2020-11-25', 20, 1003, 'transfer', 'Italian', 45, 'C');

这里我使用 ds 作为日期数据类型。

我的代码:

select ds, count(job_id) as jobs_per_day, sum(time_spent)/3600 as hours_spent 
from job_data  
where ds >='2020-11-01'  and ds <='2020-11-30'  
group by ds ;

这就是我所做的,但我必须在字符串中输入日期,然后我 无法理解如何在不使用任何内容的情况下将其转换为日期 功能。我正在使用 mysql 工作台 8.0。

【问题讨论】:

  • 如果没有函数,您无法将string 转换为date。在 Presto 中,您可以使用 date_parse prestodb.io/docs/current/functions/datetime.html#date_parse
  • 返回语法错误。 select DATE_PARSE(ds, %Y %M %D), count(job_id) as jobs_per_day, sum(time_spent)/3600 as hours_spent from job_data where ds &gt;='2020-11-01' and ds &lt;='2020-11-30' group by ds ;
  • 在 SQL 代码中,您必须使用DATE_FORMAT(ds, '%Y/%m/%d') 来选择yyyy/mm/dd 形式的日期作为字符串。 STR_TO_DATE(@parameter, '%Y/%m/%d') 如果您想以yyyy/mm/dd 形式向查询提供一个字符串作为 INSERT/UPDATE/WHERE 的值...

标签: mysql mysql-workbench presto


【解决方案1】:

如果没有函数,您无法将字符串转换为日期。在 Presto 中,您可以使用 date_parse。试试:

select cast(date_parse(ds,'%Y-%m-%d') as date) as the_date , 
count(job_id) as jobs_per_day, 
sum(time_spent)/3600 as hours_spent 
from job_data  
where the_date >='2020-11-01'  and the_date <='2020-11-30'  
group by the_date ;

【讨论】:

  • MySQL 不会使用 Presto 功能。
  • 显示错误。 FUNCTION operation_analytics.date_parse 不存在。**
  • @akina 然后是 sql server ?
  • @Chaudhari 您应该使用 Presto SQL,因为您要不要使用 Presto?
  • 即使是SQLMySQLSQL ServerPostgress等也有区别。你应该指出你正在使用的确切数据库
猜你喜欢
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多