// strings reference

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.

Категория
Strings
Returns
string
In this category
25 of 172

Синтаксис

INITCAP(string)

Поддержка движка

ДвижокSupportЗаметки
SQL Server Not built in No built-in equivalent.
PostgreSQL Да Supported under this name.
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Да Supported under this name.

Примеры

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

Полезно знать

  • 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.

Cheat sheets for the engines that have it: PostgreSQL, Oracle.

// fewer lookups

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.