Chapter 2. Function Junction
Now that we’ve looked at SELECT, we can look at adding various SQL functions to it to help us “slice and dice” the data, deal with data quality issues, and transform it to the shape we need. We’ve seen COUNT already, so we’ll start with it and the rest of the aggregate functions that often help during EDA to understand some simple statistics. In this chapter, we will cover the following topics:
-
Aggregate functions (see )
-
Conversion functions (see )
-
Cryptographic functions (see ; we won’t be using them in the way you think, though!)
-
Date and time functions (see )
-
Logical functions (see )
-
String functions (see )
-
System functions (see )
You will get very used to reading, and ideally very good at using, SQL functions by the end of this book. I take a highly functional approach to the problems presented, so something like the following will become a common sight to you:
SELECTLEFT(REPLACE(REPLACE(StreetAddress,' ',''),'.',''),10)AddressPrefixFROMcrm.CustomerAddress
Always read functions calling functions from the inside out. The previous code should read to you as follows:
-
Replace all spaces in the results from the first
REPLACE, also with an empty character. -
Replace all period (full-stop) characters in the value from the column StreetAddress with an empty character.
-
Take the left 10 characters of that result and return that truncated value in a column labeled AddressPrefix.
To illustrate most functions, we will ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access