SQL INITCAP function
Capitalises the first letter of every word and lowercases the rest. Fine for O'Brien and Jean-Luc, wrong for McDonald, iPhone and it's.
- Categoría
- Strings
- Returns
string- In this category
- 25 of 172
Sintaxis
INITCAP(string)
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Sí | Supported under this name. |
Ejemplos
PostgreSQL
SELECT INITCAP('the quiet lake') AS t; -- The Quiet Lake
PostgreSQL
SELECT INITCAP('jean-luc o''brien') AS t; -- Jean-Luc O'Brien
Oracle
SELECT INITCAP('mcdonald holdings') AS t FROM dual; -- Mcdonald Holdings
Conviene saberlo
- Every non-alphanumeric character starts a new word. That gets O'Brien and Jean-Luc right, but INITCAP('it''s') is 'It'S'.
- The rest of each word is lowercased, so INITCAP('iPhone') is 'Iphone'. Digits count as word characters, so INITCAP('3rd street') is '3rd Street'.
- SQL Server and MySQL have no INITCAP. Whole-string capitalisation is UPPER(LEFT(s,1)) + LOWER(SUBSTRING(s,2,...)); word-by-word needs a UDF or the app layer.
- Casing follows the locale: Oracle's NLS_INITCAP and PostgreSQL's collation decide how dotted and accented letters convert.
Questions about INITCAP
Is INITCAP available in SQL Server or MySQL?
No. PostgreSQL and Oracle have it; everywhere else you hand-roll it: UPPER + LOWER + SUBSTRING for the whole string, a function for word-by-word.
Why does INITCAP mangle names like McDonald?
It lowercases everything after the first letter of each word, so McDonald becomes Mcdonald. Apostrophes and hyphens start new words, which suits O'Brien but turns it's into It'S.
Should I store INITCAP output?
Rarely. Casing is presentation. Store a canonical form and INITCAP (or better, app-side formatting) at display time.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.