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.

SqlData Integrity

Code

snippet
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

FAQ

Which tables or CTEs does it reference?
The references are `accounts`.