Cleaning Data
Often, the raw data presented in a query output may not be in the desired form. You may want to remove values, substitute values, or map values to other values. To accomplish these tasks, SQL provides a wide variety of statements and functions. Functions are keywords that take in inputs (such as a column or a scalar value) and process those inputs into some sort of output. You will learn about some useful functions for data transformation and cleaning in the following sections.
The CASE WHEN Function
CASE WHEN
is a function that allows a query to map various values in a column to other values. The general format of a CASE WHEN
statement is as follows:
CASE WHEN condition1 THEN value1 WHEN condition2 THEN value2 … WHEN conditionX THEN valueX ELSE else_value END;
Here, condition1
and condition2
, through conditionX
, are Boolean conditions; value1
and value2
, through valueX
, are values to map...