0

I have the following query. I need an output which would look like this

ea_month    case    ea_year    zone_id
January     0       2013       4001
February    0       2013       4002
March       1       2013       4001
January     0       2014       4001
February    0       2014       4001
March       1       2014       4001
February    0       2014       4002
March       1       2014       4002

SELECT ea_month,CASE WHEN ea_month = (SELECT to_char(now(), 'Month')) THEN 1 ELSE 0 END,ea_year,zone_id FROM staging_ea.stg_circle_zone_billedamount_rollup

In my table I have my values for ea_month in the form of January, February, March.

After running the above query, the result-set that I am getting is something like this:

ea_month    case    ea_year    zone_id
January     0       2013       4001
February    0       2013       4002
March       0       2013       4001
January     0       2014       4001
February    0       2014       4001
March       0       2014       4001
February    0       2014       4002
March       0       2014       4002

Can anyone suggest me where am I going wrong? Its for a PostgreSQL query.

4
  • You don't need select in your CASE statement. case when ea_month = to_char(now(), 'Month') then ... should do it. Of course that is subject to environment settings: the name of the month returned by the to_char() function depends on the locale settings of the current user. Commented Mar 27, 2014 at 9:44
  • I have tried it without the SELECT as well but to no effect. Commented Mar 27, 2014 at 9:45
  • Did you make sure to_char() actually returns 'January' in English and not in some other language? What does SELECT to_char(now(), 'Month') return on its own? Commented Mar 27, 2014 at 9:55
  • SELECT to_char(now(), 'Month') returns 'March' Commented Mar 27, 2014 at 9:59

1 Answer 1

1

This is caused by the to_char() function padding the month name with spaces. This can either be fixed by applying a trim() on the result or using the FM modifier in the format mask:

SELECT ea_month,
       CASE 
          WHEN ea_month = to_char(current_date, 'FMMonth') THEN 1 
          ELSE 0 
       END,
       ea_year,
       zone_id 
FROM staging_ea.stg_circle_zone_billedamount_rollup;

See the manual for details: http://www.postgresql.org/docs/current/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIMEMOD-TABLE

Sign up to request clarification or add additional context in comments.

Comments

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.