【问题标题】:SQL to get next monday in PrestoSQL 在 Presto 下周一获得
【发布时间】:2020-03-12 03:58:47
【问题描述】:

不管sql是在哪一天执行的,我都需要得到下周一的日期。

我认为7 - day_of_week(current_date)%7 + 1 的逻辑会起作用,但是如何获取日期。

select current_date;

    _col0
1   2019-11-16


select (7 - day_of_week(current_date)%7+1)


    _col0
1   2

或者有没有其他更好的方法来做同样的事情。

我将不胜感激!

【问题讨论】:

    标签: sql datetime presto amazon-athena date-arithmetic


    【解决方案1】:

    您可以使用date_trunc 获取本周的星期一并为其添加 7 天:

    presto> select date_trunc('week', current_date) + interval '7' day;
    
       _col0
    ------------
     2019-11-18
    (1 row)
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      date_add(day, 8 - extract(day_of_week from current_date), current_date)
      

      【讨论】:

      • 谢谢。感谢你的帮助。只是一个小小的改变 - select date_add('day', 8 - extract(day_of_week from current_date), current_date)
      • @Anand:是的,检查文档我可以看到我反转了值和时间戳。固定。
      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2021-12-13
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多