Select postgresql 中两个日期之间的月份
Select month between 2 date in postgresql
例如,我有一份报告要求二月的列 Created_date
从 2022-01-26
开始并在 2022-02-25
结束
我将如何设置查询以便检索以下 table 作为响应:
**Month of the year**
Feb (((Description: Start from 2021/01/26 to 2022/02/25)))
Mar (((Description: Start from 2021/02/26 to 2022/03/25)))
Apr (((Description: Start from 2021/03/26 to 2022/04/25)))
May (((Description: Start from 2021/04/26 to 2022/05/25)))
我把描述写得通俗易懂,我不需要table。
顺便说一句,我对格式太在意了,任何有效的都行。
这将生成四行三列:
select to_char(dt, 'Mon') as month,
dt::date as start_day,
(dt::date + interval '1 month')::date as end_day
from generate_series(date '2021-01-26',
date '2021-04-26', interval '1 month') as g(dt)
如果您希望将其作为单列,则只需格式化并连接值:
select to_char(dt, 'Mon')||' (((Description: Start from '||
to_char(dt, 'yyyy/mm/dd')||' to '||
to_char((dt::date + interval '1 month'), 'yyyy/mm/dd')||')))'
from generate_series(date '2021-01-26',
date '2021-04-26', interval '1 month') as g(dt)
例如,我有一份报告要求二月的列 Created_date
从 2022-01-26
开始并在 2022-02-25
我将如何设置查询以便检索以下 table 作为响应:
**Month of the year**
Feb (((Description: Start from 2021/01/26 to 2022/02/25)))
Mar (((Description: Start from 2021/02/26 to 2022/03/25)))
Apr (((Description: Start from 2021/03/26 to 2022/04/25)))
May (((Description: Start from 2021/04/26 to 2022/05/25)))
我把描述写得通俗易懂,我不需要table。
顺便说一句,我对格式太在意了,任何有效的都行。
这将生成四行三列:
select to_char(dt, 'Mon') as month,
dt::date as start_day,
(dt::date + interval '1 month')::date as end_day
from generate_series(date '2021-01-26',
date '2021-04-26', interval '1 month') as g(dt)
如果您希望将其作为单列,则只需格式化并连接值:
select to_char(dt, 'Mon')||' (((Description: Start from '||
to_char(dt, 'yyyy/mm/dd')||' to '||
to_char((dt::date + interval '1 month'), 'yyyy/mm/dd')||')))'
from generate_series(date '2021-01-26',
date '2021-04-26', interval '1 month') as g(dt)