I have the following query that generates list of months for given year when input is date (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)

Now, instead of 2014-01-01 I need to pass only 2014 and get the same result.

有帮助吗?

解决方案

SELECT distinct to_char(dd, 'Month') FROM generate_series(to_date('2013', 'YYYY') , date_trunc('month', now()), '1 month') as dd;

其他提示

You can apped -01-01 to year input:

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top