Overlay Placing Replaces Segment
Replace a slice in place with overlay(string PLACING string FROM n [FOR len]); omitting FOR uses the inserted string own length as the replaced length.
Code
CREATE TABLE accounts (
id int PRIMARY KEY,
phone text NOT NULL
);
INSERT INTO accounts VALUES (1, '555-01-1234'), (2, '555-99-8765');
SELECT
id,
phone,
-- overlay:从第 5 位起替换 2 位为掩码
overlay(phone PLACING '***' FROM 5 FOR 2) AS masked_phone,
-- 省略 FOR:替换长度默认取插入串的长度
overlay(phone PLACING '*' FROM 5) AS default_len_mask
FROM accounts
ORDER BY id;
-- 语法形状(PostgreSQL 官方文档示例):overlay('Txxxxas' PLACING 'hom' FROM 2 FOR 4) => 'Thomas'
SELECT overlay('Txxxxas' PLACING 'hom' FROM 2 FOR 4) AS doc_example;Inside the snippet
What runs here is SELECT / INSERT / CREATE — accounts are the objects involved.
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.
Putting it to use
Run the statement against the accounts table.
Core lines
SELECT
id,
phone,
overlay(phone PLACING '***' FROM 5 FOR 2) AS masked_phone,
overlay(phone PLACING '*' FROM 5) AS default_len_mask
FROM accounts
ORDER BY id;
CREATE TABLE accounts (
id int PRIMARY KEY,
phone text NOT NULL
);
SELECT overlay('Txxxxas' PLACING 'hom' FROM 2 FOR 4) AS doc_example;
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 tables or CTEs does it reference?
- The references are `accounts`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.