BigQuery · String function
BigQuery INITCAP
INITCAP(value[, delimiters]) capitalizes the first letter of each word and lowercases the rest: INITCAP('mary-jane SMITH') returns Mary-Jane Smith. An optional second argument sets which characters separate words.
INITCAP converts a string to title case: the first character of each word is uppercased and every other letter is lowercased. Non-letters are left as they are. By default, words are separated by whitespace and a long list of punctuation, including -, _, ., ,, /, (, ), & and @. The apostrophe is not in that list, so o'neil becomes O'neil.
The optional delimiters argument replaces the default list. INITCAP(name, ' ') treats only spaces as separators, and INITCAP(name, " '") makes both spaces and apostrophes start a new word. If either argument is NULL, the result is NULL.
Because every non-initial letter is lowercased, INITCAP flattens names such as McDonald and acronyms such as USA. Use it for display, not to clean data you will compare later; compare with LOWER or UPPER instead.
Syntax
INITCAP(value[, delimiters])Arguments and return value
| Argument | Description |
|---|---|
value | The STRING to convert. |
delimiters | Optional STRING of characters that separate words. Replaces the default set of whitespace and punctuation. |
Returns: STRING. NULL if value or delimiters is NULL.
Examples
Default word separators
SELECT
INITCAP('mary-jane SMITH') AS hyphenated,
INITCAP('new_york city') AS underscore,
INITCAP('3rd street') AS starts_with_digit;| hyphenated | underscore | starts_with_digit |
|---|---|---|
| Mary-Jane Smith | New_York City | 3rd Street |
Apostrophes and custom delimiters
SELECT
INITCAP("o'neil") AS default_delimiters,
INITCAP("o'neil", " '") AS space_and_apostrophe,
INITCAP('mary-jane', ' ') AS spaces_only;| default_delimiters | space_and_apostrophe | spaces_only |
|---|---|---|
| O'neil | O'Neil | Mary-jane |
Names that title case gets wrong
SELECT
INITCAP('McDONALD') AS mixed_case_name,
INITCAP('ACME USA INC') AS acronym;| mixed_case_name | acronym |
|---|---|
| Mcdonald | Acme Usa Inc |
Common mistakes
- Expecting apostrophes to start a word. With the default delimiters
o'neilbecomesO'neil; pass" '"as delimiters forO'Neil. - Running INITCAP over curated values. It lowercases every non-initial letter, so
McDonald,iPhoneandUSAare changed. - Passing a delimiters list and forgetting the space.
INITCAP(value, '-')no longer treats spaces as separators, sonew york-citybecomesNew york-City.
FAQ
How do I convert a string to title case in BigQuery?
Use INITCAP(value). It uppercases the first letter of each word and lowercases the rest.
How do I capitalize only the first letter of a string?
CONCAT(UPPER(LEFT(value, 1)), LOWER(SUBSTR(value, 2))). INITCAP would capitalize every word.
Which characters does INITCAP treat as word separators?
By default whitespace and [ ] ( ) { } / | \ < > ! ? @ " ^ # $ & ~ _ , . : ; * % + -. Pass a second argument to use your own set.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
