【问题标题】:I need some help in a query in SQL Server [closed]我在 SQL Server 中的查询中需要一些帮助 [关闭]
【发布时间】:2020-08-31 15:17:53
【问题描述】:

我需要一个从上个月的第一天和当天的 0 小时过滤的查询。

谁能帮帮我?

【问题讨论】:

    标签: sql sql-server datetime where-clause


    【解决方案1】:

    使用日期算术:

    where mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1))
    

    表达式datefromparts(year(getdate()),month(getdate()), 1) 给出当月的第一天;然后你可以减去 1 个月来得到你想要的结果。

    如果您想过滤掉今天(及以后)的数据,那么:

    where 
        mydate >= dateadd(month, -1, datefromparts(year(getdate()),month(getdate()), 1))
        and mydate < cast(getdate() as date)
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2016-04-05
      • 2011-05-29
      • 1970-01-01
      • 2021-06-29
      • 2022-11-28
      相关资源
      最近更新 更多