Transforming Data
Often, the raw data presented in a query output may not be in the form we would like it to be. We 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 change those inputs into some sort of output. We will discuss some very useful functions for cleaning data in the following sections.
CASE WHEN
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:
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 the Boolean conditions; and else_value
is the value that is mapped if none of the Boolean...