Title-case names in SQL (INITCAP and friends)
ALL-CAPS imports like 'ANN MARIE SMITH' look wrong in a CRM. Convert them to title case at query time.
The task
Bulk imports arrive shouting. INITCAP turns 'ANN MARIE SMITH' into 'Ann Marie Smith' in one call.
The snippet
Written for PostgreSQL. Versions for MySQL and Oracle are under Variations.
SELECT INITCAP(full_name) AS display_name
FROM customers;
Fonctionnement
INITCAP uppercases the first letter of every word and lowercases the rest, treating any non-letter as a word boundary. That boundary rule cuts both ways: 'o'brien' becomes 'O'Brien', but 'mcdonald' becomes 'Mcdonald', not 'McDonald'. MySQL, SQL Server and SQLite have no INITCAP, so plain string functions can only fix the first letter of the whole string; per-word capitalization there needs a loop or application code.
Variations
Oracle
OracleSELECT INITCAP(full_name) AS display_name
FROM customers;
MySQL (first letter of the whole string only)
MySQLSELECT CONCAT(UPPER(LEFT(full_name, 1)), LOWER(SUBSTRING(full_name, 2))) AS display_name
FROM customers;
Questions that come up
Does INITCAP handle hyphenated names?
Yes: it capitalizes after any character that is not a letter or digit, so 'jean-luc' becomes 'Jean-Luc' and 'o'brien' becomes 'O'Brien'. A letter right after a digit stays lowercase ('3rd' stays '3rd'), and Mc/Mac and van/de prefixes are never handled, so spot-check real names.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.