Question

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.

Était-ce utile?

La solution

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

Autres conseils

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top