CASE

Definition

The CASE function allows you to test a number of conditional statements with arbitrary complexity. Use it when your IF functions get too long and nested.

Syntax

CASE expr
   WHEN expr_to_match THEN result
   [... more WHENs ...]
   [ELSE else_result]
END
with my_table as (
  select 'blue' as colour union all
  select 'indigo' as colour union all
  select 'blurp' as colour union all
  select 'black' as colour
)
select
  colour,
  case
    when colour = 'blue' then 'Definitely blue'
    when colour = 'indigo' then 'Kind of blue'
    when regexp_contains(colour, 'blu') then 'Maybe blue?'
    else 'Not blue'
  end as is_it_blue
from my_table
colouris_it_blue
blueDefinitely blue
indigoKind of blue
blurpMaybe blue?
blackNot blue
Subscribe to newsletter

Subscribe to receive the latest blog posts to your inbox every week.

By subscribing you agree to our Privacy Policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Start solving your organization's biggest problems with Count today.

Stay up to date with all things count, data and problem solving.