Memra

Working with text

◈ 2 cards

Upper/lower, length, concatenation, and splitting.

Reshape text

Postgres has a deep string toolkit. The everyday ones:

  • upper(s) / lower(s) — change case
  • length(s) — number of characters
  • s1 || s2 — concatenate ('a' || 'b''ab')
  • trim(s), substring(s FROM 1 FOR 3), replace(s, a, b)
  • split_part(s, delim, n) — the nth piece after splitting
SELECT name || ' (' || city || ')' AS label FROM customers;
callonresultupper(title)'Paper Boats''PAPER BOATS'length(title)'Paper Boats'11split_part(email, '@', 2)'lena@example.com''example.com'name || ' (' || city || ')''Lena Voss', 'Berlin''Lena Voss (Berlin)'
Every one of these returns a new value — the stored text is never modified. split_part is the workhorse for pulling a piece out of a structured string like an email address.
NORMAL ~/memra/learn/postgresql/string-functions utf-8 LF