【问题标题】:I have a calendar Table and I need to display only 15th and 30th for every month我有一个日历表,我只需要显示每个月的 15 日和 30 日
【发布时间】:2020-11-20 00:24:22
【问题描述】:

这是我用来显示当月最后日期的代码

SELECT * FROM Calendar_table 
WHERE date in (SELECT MAX(date) from Calendar_table
ORDER BY YEAR, 
         DATE);

【问题讨论】:

    标签: sql database oracle date where-clause


    【解决方案1】:

    您可以使用to_char()从日期中提取月份中的某天,并将其用于过滤:

    select *
    from calendar_table
    where to_char(dt, 'dd') in ('15', '30')
    

    这假定日历日期存储在列dt。

    你也可以使用extract():

    select *
    from calendar_table
    where extract(day from dt) in (15, 30)
    

    另一方面,如果你想要每月的 15 号和最后一天,那么:

    select *
    from calendar_table
    where extract(day from dt) = 15 or last_day(dt) = dt
    

    【讨论】:

    • 但是如果我们在二月使用这个逻辑,它不会选择最后一个日期。
    • @GiriRaja:你说你想要每月的 30 号,而不是每月的最后一天,因此我的回答。请参阅我的更新答案以获取替代解决方案。
    【解决方案2】:

    几天前我回答了一个与最后一天处理有关的问题,你可以用它来做同样的事情:

    https://stackoverflow.com/a/63134303/10908274

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2018-01-04
      相关资源
      最近更新 更多