【问题标题】:How to use where < current_date sql?如何使用 where < current_date sql?
【发布时间】:2016-05-12 08:24:47
【问题描述】:

我们想在我们的 PostgresSQL 数据库上运行一个查询。

Select (*)
Where
domain.blabla.date > 'yesterday'
AND
domain.blabla.date < 'current_date'

目标是只获取昨天的数据。

我们得到的错误如下:

[Err] 错误:不再支持日期/时间值“当前”

你有办法解决它吗?

非常感谢!

卢卡

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    对于The goal is to get only yesterday's data

    Select (*) 
    from domain.blabla 
    Where date_trunc('day', date) = date_trunc('day', current_date - 1);
    

    【讨论】:

      【解决方案2】:

      对于 SQL 中的当前日期,您可以使用

      SELECT CONVERT(date, getdate())
      

      您可以使用 SQL 中的昨天日期

      SELECT dateadd(day,datediff(day,1,GETDATE()),0)
      

      【讨论】:

        【解决方案3】:

        此查询将仅获取昨天的值

        select * from table_name where day > current_date- interval '1 day'
        

        或使用yesterday

        select * from table_name where day > current_date 'yesterday'
        

        SQLFiddle

        有关更多信息,请参阅PostgreSQL Date/Time Documentation

        P.S - 您不必将函数放在单引号 ' '

        【讨论】:

        • &gt; current_date 'yesterday' 给了我语法错误(即使在链接的 sqlfiddle 中),但 &gt; date 'yesterday' 刚刚工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 2018-05-13
        • 2019-04-21
        • 2013-08-22
        相关资源
        最近更新 更多