SQL title-case pattern
Capitalise the first letter of a string or of every word, per engine, and where the naive patterns fall apart (apostrophes, mid-word capitals).
- 類別
- Strings
- Returns
string- In this category
- 30 of 172
語法
UPPER(LEFT(s, 1)) + LOWER(SUBSTRING(s, 2, ...))
INITCAP(s)
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | Supported under this name. |
| SQLite | 是 | Supported under this name. |
| Oracle | 是 | Supported under this name. |
範例
SQL Server
SELECT UPPER(LEFT(name, 1)) + LOWER(SUBSTRING(name, 2, LEN(name))) AS capped FROM brands;
PostgreSQL
SELECT INITCAP(name) AS capped FROM brands; -- 'mcdonald' becomes 'Mcdonald'
SQLite
SELECT UPPER(substr(name, 1, 1)) || lower(substr(name, 2)) AS capped FROM brands;
小知識
- The one-liner UPPER(LEFT(s,1)) + LOWER(SUBSTRING(s,2,...)) capitalises the whole string's first letter only; word-by-word needs INITCAP, a UDF, or the app layer.
- INITCAP (PostgreSQL, Oracle) lowercases the rest of every word: 'iPhone' becomes 'Iphone' and 'McDonald' becomes 'Mcdonald'.
- Word boundaries are any non-alphanumeric character, so 'o'brien' and 'state-of-the-art' capitalise after apostrophes and hyphens too.
- If the casing matters, normalize on write and keep it stored; per-read title-casing costs CPU and can never be name-aware.
Questions about Title-casing a string
How do I title-case every word in SQL Server?
There is no built-in. Either accept first-letter-of-string, write a T-SQL UDF that walks the separators, or do it in the application where you have real casing rules.
Why does INITCAP produce Mcdonald?
INITCAP lowercases everything after each word's first character and treats any non-alphanumeric as a boundary; it has no dictionary of name exceptions.
Is title-casing safe for identifiers like SKU codes?
No. Codes like 'AB1C' lose their shape. Apply casing only to free-text display fields, never to codes, emails or keys.
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: SQL Server, PostgreSQL, MySQL, SQLite, 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.