Sub topic 5
Changing Case with `upper`, `lower` and `initcap`
Assume you know basic SELECT and WHERE clauses, PostgreSQL text values, and NULL. PostgreSQL provides three common functions for changing the case of text:
upper(text)changes letters to uppercase.lower(text)changes letters to lowercase.initcap(text)makes the first letter of each word uppercase and the remaining letters lowercase.
These functions return a new value. They do not change the value stored in a table unless you use them in an UPDATE.
Converting text to upper or lower case
Use upper and lower when you need a consistent representation:
SELECT upper('PostgreSQL') AS upper_name,
lower('PostgreSQL') AS lower_name;The result is POSTGRESQL and postgresql.
A common use is normalizing values before comparison. For example, this condition matches an email address without caring about its letter case:
SELECT id, email
FROM users
WHERE lower(email) = lower('[email protected]');Both sides are converted to lowercase, so values such as [email protected] and [email protected] compare equally.
The same idea works when storing normalized data:
INSERT INTO users (email)
VALUES (lower('[email protected]'));Be careful with NULL. A function applied to NULL returns NULL, not an empty string:
SELECT upper(NULL::text) AS result;The result is NULL. If you need a fallback, use coalesce:
SELECT upper(coalesce(NULL::text, 'unknown')) AS result;Formatting names with `initcap`
initcap is useful for display text that follows simple word boundaries:
SELECT initcap('mARIA de souza') AS display_name;This returns Maria De Souza.
initcap treats sequences of letters and digits as words separated by non-alphanumeric characters. It does not understand every naming convention. For example, it may produce O'neil or McDonald differently from the preferred human spelling. Treat its output as a formatting attempt, not as authoritative name data.
The function also lowercases letters after each word's first letter. Therefore, applying it to an acronym can damage the intended spelling:
SELECT initcap('postgresql API') AS result;The result is not a reliable way to preserve API. Keep official names and acronyms separately when exact capitalization matters.
Case conversion and indexes
Applying lower in a filter can prevent an ordinary index on email from being used for the expression. An expression index matches the query:
CREATE INDEX idx_users_email_lower
ON users (lower(email));
SELECT id
FROM users
WHERE lower(email) = lower('[email protected]');The indexed expression and the query expression should match. For a larger application, PostgreSQL's citext extension or a normalized column may also be appropriate, depending on how you need equality, uniqueness, and locale behavior to work.
Common errors
- Converting only one side of a comparison leaves the result case-sensitive.
- Assuming
initcapknows personal, product, or acronym capitalization rules produces incorrect display text. - Expecting
upper(NULL)orlower(NULL)to return an empty string leads to unexpectedNULLresults. - Creating an index on
emailalone does not necessarily supportWHERE lower(email) = ...; use an expression index or another deliberate design.
Exercise
What does this query return, and why does it use lower on both operands? SELECT lower('MiXeD') = lower('mixed');
Show answer
It returns true. Both strings become mixed, so the comparison does not depend on their original letter case.