INITCAP()
SQL · String Functions
Theory
INITCAP(string) converts a string to 'title case': the first letter of each word becomes uppercase and the rest of the word becomes lowercase. INITCAP('asha rao') and INITCAP('ASHA RAO') both return 'Asha Rao'. A word is any run of letters and digits, so anything else (space, hyphen, apostrophe) starts a new word.
INITCAP exists in Postgres, Oracle and Snowflake, but not in MySQL, SQL Server or SQLite. There you emulate it. For a single word, UPPER(SUBSTR(name, 1, 1)) || LOWER(SUBSTR(name, 2)) works; for two words you also find the space with INSTR and treat the second word the same way, as the diagram does. Be aware that simple title-casing is not perfect for every name: 'McDonald' becomes 'Mcdonald' and "o'brien" becomes "O'Brien". For display, that is usually good enough.
Why we use it
Names and cities often arrive in random case from forms and files: 'RAVI KUMAR', 'meera joshi'. INITCAP gives them a clean, consistent look for reports, labels and emails without changing them by hand.
When to use it
Use INITCAP for display formatting of names, cities and titles. Do not use it to compare values (use LOWER or UPPER for that). In dialects without INITCAP, emulate with SUBSTR plus UPPER/LOWER, or format in the application layer.
How it works
- Take the input string
- Read the next character
- Is it the first letter of a word? Yes: Make it uppercase. No: Make it lowercase.
- A non-letter (space, hyphen) marks the start of a new word
- Return the title-cased string
Syntax
INITCAP(string)Example
SELECT INITCAP('asha rao');
-- 'Asha Rao'Key points
- Available in Postgres, Oracle and Snowflake, not MySQL, SQL Server or SQLite
- Lowercases the rest of each word, not just capitalising the first letter
- Any non-alphanumeric character starts a new word
- Names like McDonald are not handled perfectly
- Useful for formatting names for display, not for comparison