Miscellaneous Functions (Slides)
Miscellaneous Functions (Slides)
Miscellaneous functions
Please do not copy without permission. © ExploreAI 2023.
SQL string, date, and miscellaneous functions
Data overview
| We will use the following Households_individuals table which contains certain information
about the individuals in all households in Kenya collected during a household survey in 2020.
CAST function
| The CAST() function is used to convert a value from its current data type into a specified data
type. Its basic syntax is as follows:
SELECT ● DATE
SQL keyword used to separate
CAST(expression AS datatype) ● DATETIME
the expression to be cast and TIME
FROM ●
the desired data type. DECIMAL
Table_name; ●
● INTEGER
● FLOAT
The data type to be converted
● CHAR
to.
● VARCHAR
Ensure that the value you are attempting to ● SIGNED
cast is compatible with the target data type. ● UNSIGNED
Otherwise, the function will throw an error. ● BINARY
3
SQL string, date, and miscellaneous functions
CAST function
The D_O_B column is set to the DATETIME data type. The values in the column do not have any time
information, making the time part unnecessary. The DATE data type would be more suitable here.
Query Output
4
SQL string, date, and miscellaneous functions
CONVERT function
| CONVERT() is another function that can be used for conversion from one data type to another.
Its basic syntax is as follows:
5
SQL string, date, and miscellaneous functions
CONVERT function
The Weight column has been set to the FLOAT data type. This means that the Weight values have varying
decimal precision depending on their declared values. We can convert to a DECIMAL data type with a precision
of 4 and a scale of 2 to give all the values a fixed decimal precision to avoid rounding errors in calculations.
Query Output
56 56.00
In this example, the floating-point values in the Weight
column are converted to a DECIMAL(4,2) data type 45.22 45.22
using the CONVERT() function. The results of the
operation are then stored in a new column, New_weight. 9.1 9.10
6
SQL string, date, and miscellaneous functions
IFNULL function
| The IFNULL() function returns a specified value if the given expression is null. Otherwise, it
returns the value of the expression itself. Its basic syntax is as follows:
The IFNULL() function is usually used to handle NULL values in a column or expression by replacing them with
an alternative value.
7
SQL string, date, and miscellaneous functions
IFNULL function
For records where Highest_ed readings are missing, they have been assigned a NULL value. We can
replace these NULL values with a new category called No schooling.
Query Output
SELECT
Highest_ed New_highest_ed
Highest_ed,
IFNULL(Highest_ed,‘No schooling’) AS
NULL No schooling
New_highest_ed
FROM
Household_individuals; Undergraduate Undergraduate
Primary Primary
8
SQL string, date, and miscellaneous functions
NULLIF function
| The NULLIF() function is used to compare two expressions and return NULL if they are equal.
Otherwise, the first expression is returned. Its basic syntax is as follows:
The NULLIF() function provides a way of marking certain values as NULL in an effort to treat them as missing or
unknown or to avoid particular errors.
9
SQL string, date, and miscellaneous functions
NULLIF function
On the Age column, the age of children below 1 year has been assigned 0. If we wish to exclude these records
from the aggregations performed on the Age column, we can convert the 0 values to NULL. This ensures that
the aggregation functions disregard these values.
Query Output
SELECT
Age New_age
Age,
NULLIF(Age, 0) AS New_age
0 NULL
FROM
Household_individuals;
22 22
35 35
10
SQL string, date, and miscellaneous functions
ISNULL function
| The ISNULL() function helps to determine whether an expression is NULL or not. If the
expression is NULL, this function returns 1. Otherwise, it returns 0. Its basic syntax is as follows:
SELECT
ISNULL(expression) The value to test for NULL.
FROM
Table_name;
The ISNULL() function helps when we want to filter our data or perform conditional logic based on the presence
of NULLS.
11
SQL string, date, and miscellaneous functions
ISNULL function
Suppose we want to investigate the cause behind the NULL values in Ed_institution. We can filter
our data to only remain with the NULL values in that particular column. Can you identify some
potential causes?
Query Output
SELECT
New_highes
Sex,
Sex Age t_ed
Age,
New_highest_ed
No
FROM Male 0
schooling
Household_individuals
WHERE Male 35 Masters
ISNULL(Ed_institution) = 1;
No
Female 17
The ISNULL() function in the WHERE clause checks whether the values in schooling
Ed_institution are NULL. If NULL, it returns 1, and 0 otherwise. The
WHERE clause then filters out the rows where the ISNULL() function Female 55 PHD
returns 1, i.e. Ed_institution is NULL.
12
SQL string, date, and miscellaneous functions
COALESCE function
|
The COALESCE() function evaluates a list of expressions from left to right, searching for the
first non-NULL value and returning it. If all the expressions are NULL, the function returns
NULL. Its basic syntax is as follows:
SELECT
COALESCE(expression1, expression2, expression3, ...) The list of values we want to
check for NULL.
FROM
Table_name;
The COALESCE() function allows us to handle NULL values by providing an alternative or fallback value.
13
SQL string, date, and miscellaneous functions
COALESCE function
The Spouse column seems redundant since an individual will automatically have a spouse if married,
or no spouse if single. We can combine Spouse and Spouse_ID to form a new column that reads the
string ‘N/Aʼ if one is single or the spouseʼs ID if married.
Query Output
SELECT
Marital_status, Marital_ Spouse_ New_spo
Spouse_ID, status ID Spouse use_ID
Spouse,
COALESCE(Spouse_ID, Spouse) AS New_spouse_ID Single NULL N/A N/A
FROM
Single NULL N/A N/A
Household_individuals;
Married 3331 Yes 3331
The COALESCE function starts by checking the Spouse_ID column and if
its value is not NULL, it will be assigned as the value for the new column, Single NULL N/A N/A
New_spouse_ID. However, if the Spouse_ID value is NULL, the function
will move on to evaluate the Spouse column for a non-null value. It's Married 3891 Yes 3891
value, which in this case is the string ‘N/Aʼ, is then returned in the new
column.
14