Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top