Create list with first and last day of month for given period

前端 未结 2 408
谎友^
谎友^ 2021-01-19 10:46

I have to generate a list with two columns of day intervals for every month in a specific period. First column must be the first day of month and the second column the last

相关标签:
2条回答
  • 2021-01-19 11:20

    Based on this answer:

    select d::date as start_date,(d + '1 month'::interval - '1 day'::interval )::date end_date
    from generate_series('2014-01-01'::date, '2014-06-30'::date, '1 month'::interval) d
    
    0 讨论(0)
  • 2021-01-19 11:28

    Add a month and subtract a day - in a single interval expression:

    SELECT d AS mon_first
         , d + interval '1 month - 1 day' AS mon_last
    FROM   generate_series(timestamp '2014-01-01'
                         , timestamp '2014-06-30'
                         , interval  '1 month') d;
    • The manual about interval input

    Further reading ("end of day" is very similar to "last day of month"):

    • How to get the end of a day?

    About generating a time series:

    • Generating time series between two dates in PostgreSQL
    0 讨论(0)
提交回复
热议问题