1

I'm using CASE to clean up some state abbreviations, in a table, but it's working contrary to the logic. I selected the length alone to show that the length is being calculated correctly, so I think it's the CASE logic that's off

When I query...

SELECT billing_state,
       length(billing_state),
       CASE billing_state
         WHEN length(billing_state) > 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
         WHEN length(billing_state) = 2 THEN upper(billing_state)
         ELSE 'UNKNOWN'
       END as billing_state_fixed           
  FROM accounts
+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA            |                     2 | NULL                |
| Alabama       |                     7 | ALABAMA             |
| MS            |                     2 | NULL                |
| FL            |                     2 | NULL                |
| NULL          |                  NULL | UNKNOWN             |
+---------------+-----------------------+---------------------+

However, when I enter this bizarro logic, it works.

SELECT billing_state,
       length(billing_state),
       CASE billing_state
         WHEN length(billing_state) = 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
         WHEN length(billing_state) <> 2 THEN upper(billing_state)
         ELSE 'UNKNOWN'
       END as billing_state_fixed           
  FROM accounts

+---------------+-----------------------+---------------------+
| billing_state | length(billing_state) | billing_state_fixed |
+---------------+-----------------------+---------------------+
| GA            |                     2 | GA                  |
| Alabama       |                     7 | AL                  |
| MS            |                     2 | MS                  |
| FL            |                     2 | FL                  |
| NULL          |                  NULL | UNKNOWN             |
+---------------+-----------------------+---------------------+

Can anyone take a swing at this one?

2
  • I don't think you need billing_state in your CASE billing_state WHEN length(billing_state) expression. Commented Oct 15, 2012 at 20:04
  • It looks like you are using a hybrid form of the case expression. There are 2 types. One with a CASE <expression>, another with CASE WHEN <expression>. Try removing billing_state from CASE billing_state. Commented Oct 15, 2012 at 20:05

1 Answer 1

1

Per the docs, your syntax isn't quite correct.

You've muddle CASE value WHEN compare_value and CASE WHEN expression.

What you probably want is:

SELECT billing_state,
       length(billing_state),
       CASE
         WHEN length(billing_state) > 2 THEN (select state_abbr from lkup_states where upper(state_name) = billing_state)
         WHEN length(billing_state) = 2 THEN upper(billing_state)
         ELSE 'UNKNOWN'
       END as billing_state_fixed           
  FROM accounts
Sign up to request clarification or add additional context in comments.

2 Comments

Geez. Thanks, Dan. I looked at the docs, but my eyes went straight for 'CASE value WHEN' and totally ignored 'CASE WHEN'. Thanks again.
@ScoresOracle no worries! sometimes you just need that second pair of eyes :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.