PostgreSQL INITCAP Function (6 Examples)

Six contacts arrived from a web form as hANNAH BROOKS and TYLER O'BRIEN. The PostgreSQL initcap function fixes them in one call: initcap('hANNAH BROOKS') returns Hannah Brooks.

The catch: initcap knows nothing about names or acronyms. McDonald came back as Mcdonald, and III as Iii.

Six examples follow, from one string to separators, state codes and an UPDATE. Tested on PostgreSQL 18.6 with psql in VS Code.

PostgreSQL INITCAP Function Syntax

Here is the syntax:

initcap ( text ) → text

It uppercases the first letter of each word and lowercases the rest, where a word is any run of letters and digits and every other character, even an apostrophe, ends one.

Return value: text. NULL in, NULL out.

Availability: every supported PostgreSQL release.

When to Use the INITCAP Function in PostgreSQL

Use initcap in PostgreSQL when you need to:

Run this once. Every example uses these six US contacts:

CREATE TABLE contacts (
  id        int PRIMARY KEY,
  full_name varchar(20),
  city      varchar(16),
  state     char(2)
);
INSERT INTO contacts VALUES
 (1, 'hANNAH BROOKS',   'columbus',      'oh'),
 (2, 'TYLER O''BRIEN',  'SAN ANTONIO',   'tx'),
 (3, 'grace mcdonald',  'st. louis',     'mo'),
 (4, 'mary-ann ellis',  'winston-salem', 'nc'),
 (5, 'marcus reed iii', 'NEW YORK',      'ny'),
 (6, 'Olivia Hayes',    NULL,            'WA');

Example 1: Capitalize the First Letter of Each Word

Start small. You want Hannah’s name fixed, plus a NULL test:

\pset null '(null)'

SELECT initcap('hANNAH BROOKS') AS name,
       initcap(NULL)            AS null_in;

Output:

     name      | null_in 
---------------+---------
 Hannah Brooks | (null)
(1 row)

That returns Hannah Brooks; NULL stays NULL. initcap lowercased ANNAH by itself, so you don’t need PostgreSQL lower() first.

Example 2: Proper Case Names in a PostgreSQL Table

Now the table. The goal is clean full_name values for display:

\pset footer off

SELECT initcap(full_name) AS full_name
FROM contacts
ORDER BY id;

Output:

    full_name    
-----------------
 Hannah Brooks
 Tyler O'Brien
 Grace Mcdonald
 Mary-Ann Ellis
 Marcus Reed Iii
 Olivia Hayes
PostgreSQL initcap on full_name returning Tyler O'Brien, Grace Mcdonald and Marcus Reed Iii
Mcdonald and Iii need a human.

For Tyler, this returns Tyler O'Brien, and the hyphen gives Mary-Ann two capitals. Two rows don’t survive, though: Grace Mcdonald and Marcus Reed Iii.

Example 3: How INITCAP Splits Words (Apostrophes, Hyphens, Digits)

Say you need to predict odd input:

SELECT s AS input, initcap(s) AS initcap
FROM (VALUES ('pat''s diner'), ('mary-ann'),
             ('3rd street'), ('ibm usa llc'),
             ('first_name')) AS t(s);

Output:

    input    |   initcap   
-------------+-------------
 pat's diner | Pat'S Diner
 mary-ann    | Mary-Ann
 3rd street  | 3rd Street
 ibm usa llc | Ibm Usa Llc
 first_name  | First_Name
(5 rows)
PostgreSQL initcap returning Pat'S Diner, Mary-Ann, 3rd Street, Ibm Usa Llc and First_Name
Every non-letter, non-digit starts a new word.

pat's diner returns Pat'S Diner. The same rule that fixed O’Brien breaks every possessive.

Digits belong to the word, so 3rd stays lowercase after the 3, while the underscore in first_name starts a new word and hands you First_Name. For targeted fixes, I have a separate guide on PostgreSQL REGEXP_REPLACE.

Example 4: Proper Case the City but Keep State Codes Uppercase

I want City, ST for a label:

SELECT initcap(city) || ', ' || initcap(state) AS wrong,
       initcap(city) || ', ' || upper(state)   AS mailing
FROM contacts
WHERE id IN (1, 4);

Output:

       wrong       |      mailing      
-------------------+-------------------
 Columbus, Oh      | Columbus, OH
 Winston-Salem, Nc | Winston-Salem, NC
(2 rows)

For Columbus, initcap gives Columbus, Oh; upper() gives Columbus, OH. The same pieces work with PostgreSQL CONCAT.

Example 5: Capitalize Only the First Letter of a String in PostgreSQL

Sometimes title case is wrong. The goal is sentence case for an order note:

SELECT initcap(s) AS initcap,
       upper(left(s, 1)) || lower(substr(s, 2)) AS first_only
FROM (VALUES ('LATE DELIVERY TO OHIO')) AS t(s);

Output:

        initcap        |      first_only       
-----------------------+-----------------------
 Late Delivery To Ohio | Late delivery to ohio
(1 row)
PostgreSQL initcap returning Late Delivery To Ohio next to upper(left(s,1)) || lower(substr(s,2)) returning Late delivery to ohio
initcap vs first letter only.

first_only returns Late delivery to ohio. Ohio loses its capital, so use it for notes, not places.

left() grabs the first character and substr(s, 2) grabs everything after it, the same positions the PostgreSQL SUBSTRING function uses.

Example 6: Clean a Column with UPDATE and INITCAP

Last, store the fix. You want full_name cleaned, touching only messy rows:

UPDATE contacts
SET full_name = initcap(full_name)
WHERE full_name IS DISTINCT FROM initcap(full_name);

SELECT count(*) AS still_messy FROM contacts
WHERE full_name <> initcap(full_name);

Output:

UPDATE 5
 still_messy 
-------------
           0
(1 row)
PostgreSQL UPDATE contacts SET full_name = initcap(full_name) reporting UPDATE 5 and still_messy 0
Five rows fixed, Olivia skipped.

psql reports UPDATE 5, and still_messy is 0, because the WHERE skipped Olivia’s clean row. Run the SELECT again and you should still see 0. My PostgreSQL UPDATE walkthrough has more patterns.

INITCAP vs UPPER and LOWER: Which Should You Use?

FunctionUse it for
initcapnames, cities, labels
upperstate codes, SKUs
loweremails, matching

My rule of thumb: initcap for display, lower for matching.

Tips & Common Mistakes

  • initcap takes text only. initcap(42) failed with function initcap(integer) does not exist; cast with 42::text.
  • It keeps extra spaces, so trim first.
  • Mc names and acronyms need a manual pass or REPLACE in PostgreSQL.
  • The collation changes results. Under COLLATE "C", élodie became éLodie; the ICU collation en-US-x-icu turned o'brien into O'brien.

Wrapping Up

In PostgreSQL, copy Example 4 for addresses and Example 6 to clean stored data.

Next step: browse the complete list of PostgreSQL functions or the PostgreSQL 18 string functions docs.

Two more string functions for the same cleanup job:

Frequently Asked Questions

Is there an INITCAP function in SQL Server or MySQL?

No. Both need UPPER(LEFT(…)) plus LOWER(SUBSTRING(…)) or a user-defined function. Oracle’s INITCAP works like PostgreSQL’s.

Can I use initcap in a generated column?

Yes, it’s IMMUTABLE. GENERATED ALWAYS AS (initcap(n)) STORED returned Tyler Reed in my test.

How do I keep words like and, of and the lowercase?

initcap has no option. Wrap it in a small PL/pgSQL function that lowercases a word list.