在PostgreSQL的范围内获取日期列表 [英] Getting date list in a range in PostgreSQL

查看:131
本文介绍了在PostgreSQL的范围内获取日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PostgreSQL数据库中获取两个日期之间的日期列表(包括它们)。例如:如果我有:




  • 开始日期:2012年6月29日

  • 结束日期:3那么结果应该是:

      code> 2012年6月29日
    2012年6月30日
    2012年7月1日
    2012年7月2日
    2012年7月3日

    在PostgreSQL中最好的方法是什么?



    谢谢。

    解决方案

     选择CURRENT_DATE + i 
    from generate_series(date'2012-06-29'- CURRENT_DATE,
    date'2012-07-03' - CURRENT_DATE)i

    甚至更短: / p>

      select i :: date from generate_series('2012-06-29',
    '2012-07-03' ,'1天'::间隔)i


    I'd like to get the list of days between the two dates (including them) in a PostgreSQL database. For example, if I had:

    • start date: 29 june 2012
    • end date: 3 july 2012

    then the result should be:

    29 june 2012
    30 june 2012 
    1 july 2012 
    2 july 2012 
    3 july 2012
    

    What would be the best way of doing this in PostgreSQL?

    Thanks.

    解决方案

    select CURRENT_DATE + i 
    from generate_series(date '2012-06-29'- CURRENT_DATE, 
         date '2012-07-03' - CURRENT_DATE ) i
    

    or even shorter:

    select i::date from generate_series('2012-06-29', 
      '2012-07-03', '1 day'::interval) i
    

    这篇关于在PostgreSQL的范围内获取日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆