INITCAP TRANSLATE Text Cleanup
Normalize messy contact strings with INITCAP for word-initial capitalization and TRANSLATE for per-character masking or deletion via an empty replacement set.
Code
CREATE TABLE contacts (
id int PRIMARY KEY,
raw text NOT NULL
);
INSERT INTO contacts VALUES
(1, 'ADA LOVELACE'),
(2, 'grace hopper'),
(3, 'hotline 555-01-1234');
-- text cleanup:INITCAP 每个词首字母大写、其余转小写;
-- TRANSLATE 把 from 集合里的字符逐个映射,映射到空串即删除
SELECT
id,
INITCAP(raw) AS display,
TRANSLATE(raw, '0123456789', '**********') AS masked,
TRANSLATE(raw, '0123456789-', '') AS digits_only
FROM contacts
ORDER BY id;How the snippet works
SELECT / INSERT / CREATE statements share the script, touching contacts.
Mechanically, the statements are built from: an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.
Using it
The contacts table is the working surface here.
Key statements
SELECT
id,
INITCAP(raw) AS display,
TRANSLATE(raw, '0123456789', '**********') AS masked,
TRANSLATE(raw, '0123456789-', '') AS digits_only
FROM contacts
ORDER BY id;
INSERT INTO contacts VALUES
(1, 'ADA LOVELACE'),
(2, 'grace hopper'),
(3, 'hotline 555-01-1234');
CREATE TABLE contacts (
id int PRIMARY KEY,
raw text NOT NULL
);
Interactive Tools for This Topic
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- Which objects does the SQL touch?
- `contacts` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.