4.8/5
Home

Integrations

Plug Count neatly into your existing data infrastructure

For data teams

Collaborative, AI-driven data exploration and modelling

For product & growth

Faster access to the answers which matter

For leaders

Turning data into an engine of improvement

Pricing

Latest post

Meet the new Count Agent

Meet the new Count Agent

Docs

Guides, references, and API docs

Blog

Opinions, analysis, and the occasional uncomfortable truth about data.

Webinars

Join our experts for deep dives into data analytics and business intelligence

Gallery

Browse ready-to-use canvas templates and try them instantly

Sign in

Where to begin?

Start for free

Spin up a canvas and explore your data — a couple of clicks, no card.

Book a demo

See it live. Bring your questions — we’ll bring the answers.

Still not sure?

Send us a note — we’ll point you the right way.

Data to decisions, faster.

Compare

  • Count vs BI
  • Count vs Notebooks
  • Count vs Chatbots
  • Count vs Hex
  • Count vs Looker
  • Count vs Tableau
  • Count vs Thoughtspot

Learn

  • Blog
  • Webinars
  • SQL tutorials

Legal & security

  • Privacy Policy
  • Terms of Use
  • Cookies Policy
  • Trust Center
  • Security

Social

  • LinkedIn →
  • YouTube →
  • X →

Start with the hard question.

Ask Count anything — we’ll take you straight into a canvas.

Opens sign-up with your prompt ready for Count.

© 2026 Count Technologies Ltd. All rights reserved.

Careers →Book a Demo →
count

SQL Resources

Count is the best SQL IDE, wrapped up in the best data analysis tool, all inside the best BI platform.

ARRAY_AGG
Arrays Explained
Arrays Explained
CASE
DefinitionPractical InfoCommon QuestionsTroubleshooting Common ErrorsRelated Pages
CAST
COALESCE
CONCAT
COUNT [DISTINCT]
CURRENT_DATE
CURRENT_DATETIME
CURRENT_TIMESTAMP
DATETIME_DIFF
DATETIME_TRUNC
DATE_DIFF
DATE_TRUNC
Data Types
Dates and Times
EXTRACT
IF
MEDIAN
SUBSTR
String Functions Explained
TIMESTAMP_DIFF
TIMESTAMP_TRUNC
TIME_DIFF
TIME_TRUNC
UNNEST
Window Functions Explained
SQL Resources/BigQuery Standard SQL/CASE

CASE

CASE function. Definition, syntax, examples and common errors using BigQuery Standard SQL. The CASE function allows you to perform conditional statements in SQL.

Definition

The CASE function allows you to perform conditional statements in BigQuery. This is a more complex implementation of IF.

colourprimary_colour
bluetrue
pinkfalse
blackfalse

Practical Info

  • If you leave out an else_result, then the query will return NULL if not matched to the result
  • result and else_result must be coercible to a common supertype.

Common Questions

How to create multiple 'THEN' clauses for a case statement?

To add multiple expression -> result pairings, simply add another WHEN ... THEN clause (without commas) to your function.

Using the example data above, we could use the following query to identify which are the primary colours:

CASE expr
WHEN expr_to_match THEN result
[...]
[ELSE else_result]
END

How to compare strings ignoring case?

SELECT
  colour,
  CASE WHEN colour IN ('blue', 'red', 'yellow') THEN true ELSE false END AS primary_colour
FROM
  (
    SELECT
      'blue' AS colour
    UNION ALL
(    SELECT
      'pink' AS colour)
    UNION ALL
(    SELECT
      'black' AS colour)
  ) AS table_3

To account for strings being of different cases, we can use UPPER and LOWER to standardise all of the cases in both our expressions to match, and the match criteria.

Using the data above, we could modify our case statement as follows:

CASE WHEN colour ='blue' THEN true
WHEN colour = 'red' THEN true
WHEN colour = 'yellow' then true
ELSE false
END AS primary_colour

How to compare dates using a case statement?

CASE
WHEN UPPER(colour) IN ('BLUE', 'RED', 'YELLOW') THEN true
ELSE false
END AS primary_colour

As long as your expressions are of the right type you can use comparison operators (e.g. >, <, >=) to compare dates:

CASE
WHEN date_col BETWEEN '2020-10-01' and '2021-09-30' THEN 'FY 2021'
WHEN date_col<'2020-10-01' then 'pre FY 2021'
END fiscal_date
ℹ️

Note: BETWEEN is inclusive so it will return true for both 2020-10-01 AND 2021-09-30.

date_colfiscal_date
2020-10-01FY 2021
2021-09-30FY 2021
2019-10-01pre FY 2021

How to Group By / Aggregate by a Case function

To group by the results of a CASE function, just make sure you add the name of the column to your GROUP BY clause.

f0_Region
-1East
4West

To aggregate by a case column, make sure your results are numbers:

SELECT
  date_col,
  CASE WHEN (date_col BETWEEN '2020-10-01' AND '2021-09-30') THEN 'FY 2021' WHEN (date_col < '2020-10-01') THEN 'pre FY 2021' END AS fiscal_date
FROM
  (
    SELECT
      '2020-10-01' AS date_col
    UNION ALL
(    SELECT
      '2021-09-30' AS date_col)
    UNION ALL
(    SELECT
      '2019-10-01' AS date_col)
  ) AS table_3
east_totalwest_total
-14

Troubleshooting Common Errors

Type mismatch for operands of = operator: number and string.

This happens when your conditional expression is invalid. For example:

SELECT
  SUM(number),
  CASE WHEN city IN ('New York', 'Miami') THEN 'East' ELSE 'West' END AS Region
FROM
  (
    SELECT
      2 AS number,
      'New York' AS city
    UNION ALL
(    SELECT
      4 AS number,
      'Los Angeles' AS city)
    UNION ALL
(    SELECT
      (-3) AS number,
      'Miami' AS city)
  ) AS table_3
GROUP BY
  Region

You can resolve this by making sure your condition column and criteria are compatible types:

SELECT
  SUM(CASE WHEN city IN ('New York', 'Miami') THEN number ELSE 0 END) AS east_total,
  SUM(CASE WHEN city IN ('Los Angeles') THEN number ELSE 0 END) AS west_total
FROM
  (
    SELECT
      2 AS number,
      'New York' AS city
    UNION ALL
(    SELECT
      4 AS number,
      'Los Angeles' AS city)
    UNION ALL
(    SELECT
      (-3) AS number,
      'Miami' AS city)
  ) AS table_3

Case expression returns with mixed types are not supported.

This error occurs when your result types are different. In this example returning 1 in one case and 'hello' in another won't work.

CASE WHEN number_column = 'string' ....

To resolve this, make sure your result types are compatible and coercible.

Related Pages

  • Data Types in Standard SQL
CASE WHEN CAST(number_column as STRING) = 'string' ...
CASE WHEN (CAST(bq.Avg_monthly_searches AS STRING) = 'total') THEN 1 ELSE 'hello' END
CASE WHEN (CAST(bq.Avg_monthly_searches AS STRING) = 'total') THEN '1' ELSE 'hello' END

Got a CSV?
See it differently in <2 mins

Get started for FREEWatch our CEO do it