【问题标题】:First day of the month每月第一天
【发布时间】:2020-10-02 20:18:40
【问题描述】:
WHERE to_date(smz_loanservicing.as_of_date) = last_day(add_months(current_date, -1))

仅当loanservicing.as_of_date 发生在当月的最后一天时,以上才会提供数据。

上个月(2020 年 5 月 31 日)的最后一天是星期日。

有没有办法获取当月的第一天,如果这个特定的日期发生在当月的第一天和最后一天之间,显示日期?周日基本上没有活动,所以数据被遗漏了。 我试过了

to_date(smz_loanservicing.as_of_date) 
    between first_day(add_month(current_date,-1)) 
    and last_day(add_months(current_date, -1))` 

但是我得到语法错误。

【问题讨论】:

  • 请用您正在使用的数据库标记您的问题:这是oracle吗?
  • as_of_date的数据类型是什么?

标签: sql date where-clause


【解决方案1】:

您似乎想检查您的日期列是否属于当前月份。

您使用的语法可以在 Oracle 中使用,所以让我假设这是您正在运行的数据库。我还假设列as_of_date 的数据类型为date(或timestamp)。

你的要求应该很简单:

where
    as_of_date >= trunc(current_date, 'mm') 
    and as_of_date < add_months(trunc(current_date, 'mm'), 1)

实际上,您的语法也可以在 Snowflake 中使用 - 以上代码也是如此。

注意:如果as_of_date实际上是一个字符串,那么你需要to_date()来转换它。

【讨论】:

    【解决方案2】:

    您可以将日期截断为月份,然后您不需要知道第一天或最后一天。

    where trunc(as_of_date, ‘MM’) = trunc(current_date, ‘MM’)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      相关资源
      最近更新 更多