【问题标题】:How to filter records of people who have a birthday between two dates based on their birthday using Firebird?如何使用 Firebird 根据生日过滤在两个日期之间生日的人的记录?
【发布时间】:2021-01-01 01:34:04
【问题描述】:

我想在我的数据库中查询生日在开始日期和结束日期之间的人。该字段与数据库中的日期一起存储,因此需要选择周年纪念日而不考虑年份。我尝试使用提取之类的函数来获取日期和月份,但是当开始日期大于最后一天时,不显示任何记录。有没有人有没有存储过程的解决方案?

【问题讨论】:

    标签: sql firebird firebird-3.0


    【解决方案1】:

    如果要忽略年份,则需要提取月份和日期并分别比较:

    • 月份是开始月份,日期等于或大于开始日期
    • 月份大于开始月份,小于结束月份
    • 月份为月底,天数小于或等于结束日

    这会给你这个查询:

    select * 
    from (
        select 
            extract(month from people.birthdate) as birthmonth, 
            extract(day from people.birthdate) as birthday, 
            * 
        from people) a
    where a.birthmonth = :startmonth and a.birthday >= :startday
    or a.birthmonth > :startmonth and a.birthmonth < :endmonth
    or a.birthmonth = :endmonth and a.birthday <= :endday
    

    请注意,为了清楚起见,我使用了命名参数,即使 Firebird 本身没有它们。您需要将其替换为位置参数,除非您的连接库可以为您执行此操作。

    如果您想提供一个实际的开始和结束日期作为参数,那么您可能需要做一些额外的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 2020-01-31
      • 2012-07-09
      • 2010-12-30
      • 1970-01-01
      相关资源
      最近更新 更多