LENGTH, UPPER, LOWER
Part of the Beyond the Basics section of Coddy's SQL journey — lesson 6 of 27.
Three small functions you'll use everywhere on text columns.
LENGTH(s): number of characters insUPPER(s): same string in upper caseLOWER(s): same string in lower case
SELECT name, LENGTH(name) AS chars, UPPER(name) AS upper_name
FROM usersThey take a string and return one. That makes them perfect for normalising messy data inside a WHERE clause: WHERE LOWER(email) = 'bob@x.com' matches 'BOB@X.COM' too.
Challenge
EasyAvailable tables and columns:
<strong>countries</strong>:<strong>code</strong>,<strong>name</strong>
The code column has inconsistent casing. Return only countries whose name is between 5 and 7 characters long (inclusive), with:
code: upper-casedname: lower-casedname_length: character count
Order alphabetically by the upper-cased code.
Cheat sheet
Common string functions for text columns:
LENGTH(s): number of characters insUPPER(s): converts string to upper caseLOWER(s): converts string to lower case
SELECT name, LENGTH(name) AS chars, UPPER(name) AS upper_name
FROM usersUseful for normalising data in WHERE clauses:
WHERE LOWER(email) = 'bob@x.com'Try it yourself
SELECT -- cleaned code, lowered name, length
FROM countries
WHERE -- name length between 5 and 7
ORDER BY -- cleaned code
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Beyond the Basics
2String Functions
LENGTH, UPPER, LOWERSUBSTRINSTRREPLACE and TRIMConcatenating with ||Recap - Invoices