wheeler
wheeler

Reputation: 3241

Combining different CASE expression syntaxes

My understanding of a CASE expression you can use it for matching:

CASE expression
    WHEN first_option THEN some_value
    WHEN second_option THEN some_other_value
    ELSE default_value
END

and for evaluating conditions:

CASE
    WHEN a_value = another_value THEN some_result
    WHEN some_other_condition THEN some_other_result
    ELSE default_value
END

but can you combine both:

CASE expression
    WHEN first_option AND a_value = another_value THEN some_result
    WHEN second_option AND some_other_condition THEN some_other_result
    ELSE default value
END

For example, will this work?:

CASE btt_display
    WHEN 'COL' AND bd_trans_date = bd_posted_date THEN 'Y'
    WHEN 'DIS' AND bd_trans_date = bs_posted_date THEN 'Y'
    ELSE 'N'
END as sda_ind

Upvotes: 1

Views: 30

Answers (1)

Aaron Dietz
Aaron Dietz

Reputation: 10277

You can't combine them. One expects a value after WHEN, the other a condition.

CASE column WHEN value
CASE WHEN condition

Attempting to combine them places a condition where a value is expected.

Upvotes: 1

Related Questions