我有以下查询,当输入为日期(2014-01-01
)时,它会生成给定年份的月份列表:
SELECT to_char(dd, 'Month')
FROM generate_series(CAST('2014-01-01' AS DATE), date_trunc('month', now()), '1 month') as dd;
to_char
-----------
January
February
(2 rows)
现在,我只需要通过2014
并获得相同的结果,而不是2014-01-01
。
SELECT distinct to_char(dd, 'Month') FROM generate_series(to_date('2013', 'YYYY') , date_trunc('month', now()), '1 month') as dd;
您可以应用-01-01
到年份输入:
SELECT
to_char(dd, 'Month')
FROM
generate_series(
('2013' || '-01-01')::date,
('2013' || '-01-01')::date + interval '1 year' - interval '1 day',
'1 month') as dd;