← Back to PostgreSQL, page 1

postgresql-0013 · Easy fresher

Use string functions to clean and format text: trim, initcap, split_part, regexp_replace and format.

trim removes surrounding whitespace, lower and initcap normalise case, split_part takes a delimited field, regexp_replace rewrites patterns, and format builds strings from placeholders, with separate ones for values and for identifiers. || concatenates but yields NULL if any part is NULL, while concat and concat_ws skip NULLs.

Full answer

Test data:

sql
CREATE TABLE signups (raw_name text, email text, phone text, city text);
INSERT INTO signups VALUES
  ('  asha RAO ', 'Asha.Rao@Example.IN', '+91 98450-12345', NULL),
  ('ben  thomas', 'ben@mail.co', '080 2222 3333', 'Pune');
sql
SELECT initcap(trim(raw_name)) AS name, lower(email) AS email, split_part(lower(email), '@', 2) AS domain, regexp_replace(phone, '[^0-9]', '', 'g') AS digits FROM signups ORDER BY 1;

Output:

text
     name    |        email        |   domain   |    digits
-------------+---------------------+------------+--------------
 Asha Rao    | asha.rao@example.in | example.in | 919845012345
 Ben  Thomas | ben@mail.co         | mail.co    | 08022223333
(2 rows)
sql
SELECT raw_name || ' from ' || city AS piped, concat_ws(', ', trim(raw_name), city) AS with_ws, format('%s lives in %s', trim(raw_name), coalesce(city, 'unknown')) AS formatted FROM signups ORDER BY 1;

PostgreSQL returns:

text
         piped         |      with_ws      |         formatted
-----------------------+-------------------+---------------------------
 ben  thomas from Pune | ben  thomas, Pune | ben  thomas lives in Pune
 NULL                  | asha RAO          | asha RAO lives in unknown
(2 rows)

trim and initcap turn ' asha RAO ' into 'Asha Rao'; initcap capitalises each word, so names such as 'McDonald' need care. regexp_replace with the 'g' flag strips every non-digit from the phone numbers. In the second query, the || expression is NULL for Asha because her city is NULL, while concat_ws skips the NULL and format with coalesce gives a full sentence. Rows sort NULL last by default in ascending order, which is why Ben's row comes first here.

sql
CREATE TABLE signups (raw_name text, email text, phone text, city text);
INSERT INTO signups VALUES
  ('  asha RAO ', 'Asha.Rao@Example.IN', '+91 98450-12345', NULL),
  ('ben  thomas', 'ben@mail.co', '080 2222 3333', 'Pune');

SELECT initcap(trim(raw_name)) AS name, lower(email) AS email, split_part(lower(email), '@', 2) AS domain, regexp_replace(phone, '[^0-9]', '', 'g') AS digits FROM signups ORDER BY 1;

SELECT raw_name || ' from ' || city AS piped, concat_ws(', ', trim(raw_name), city) AS with_ws, format('%s lives in %s', trim(raw_name), coalesce(city, 'unknown')) AS formatted FROM signups ORDER BY 1;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • trim, lower and initcap normalise text
  • split_part extracts fields
  • regexp_replace with the g flag
  • || propagates NULL
  • concat_ws and format handle NULLs and formatting

Common mistakes

  • Concatenating nullable columns with ||
  • Trusting initcap for all names

Follow-ups

  • How would you build dynamic SQL safely with format()?
  • How do you split a string into rows?

Tips

  • Show the NULL concatenation