【问题标题】:Add one day to Now() and return as default value in SQL query将一天添加到 Now() 并作为 SQL 查询中的默认值返回
【发布时间】:2022-11-28 20:05:06
【问题描述】:

我正在尝试将一天添加到 NOW() 并作为列的值返回。 这有效

SELECT NOW() as date

但这给出了一个错误

SELECT DATE_ADD( NOW(), INTERVAL 1 DAY) as date

有没有办法在 postgres 查询中实现这一点? 谢谢

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    如果您提到遇到的错误会更容易。我认为 PostgreSQL 中没有 date_add() 函数:

    ERROR:  function date_add(timestamp with time zone, interval) does not
    exist
    LINE 1: select date_add(now(), interval '1 day');
                   ^
    HINT:  No function matches the given name and argument types. You
    might need to add explicit type casts.
    

    但是您可以使用常规的 + 运算符将 interval 添加到 timestamptz ,由 now() 返回。 Demo:

    select now() + '1 day'::interval;
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2016-05-12
      • 2019-04-05
      相关资源
      最近更新 更多