1

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.

0

2 Answers 2

2
SELECT distinct to_char(dd, 'Month') FROM generate_series(to_date('2013', 'YYYY') , date_trunc('month', now()), '1 month') as dd;
Sign up to request clarification or add additional context in comments.

6 Comments

thanks but the o/p has the additional 2 values of January and February each
@AmitSharad Sorry, i did not understand what you just said: thanks but the o/p has the additional 2 values of January and February each
If you input year as 2013 , it will return additional January ad February , please verify
@AmitSharad So you want to get distinct values of month ? Please update your question to be more specific.
@Houri Yes you're correct the distinct Month values , anyways I will accept your answer. Thank you for your comments and time on this :)
|
2

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;

4 Comments

thanks but the o/p has the additional 2 values of January and February each
@AmitSharad Please be more specific. My query returns two rows, you expect different number of rows?
If you input year as 2013 , it will return additional January ad February , please verify
@AmitSharad Fixed to adjust for different year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.