Postgresql列出当月(或某几月)所有日期

有时候,我们需要构造一个自然天的列作为temp表来join其他表数据,列出当月(或某几月)所有日期:

select to_char(tt.day, 'yyyy-mm-dd') as day

from (select generate_series(cast(to_char(current_date, 'yyyy-mm') || '-01' as date),

cast(cast(to_char(current_date, 'yyyy-mm') || '-01' as timestamp) + '1 MONTH' + '-1 d' as

date), '1 d') as day) as tt

order by day;


select to_char(tt.day, 'yyyy-mm-dd') as orderDate
from (select generate_series(cast(to_char(to_date('2018-07-11', 'YYYY-MM'), 'yyyy-mm') || '-01' as date),
                             cast(
                               cast(to_char(to_date('2018-08-12', 'YYYY-MM'), 'yyyy-mm') || '-01' as timestamp) + '-1 d'
                               as date), '1 d') as day) as tt
order by orderDate;

 

你可能感兴趣的:(Postgresql)