LTRIM And RTRIM Strip Any Set Character

Avoid prefix/suffix corruption from LTRIM and RTRIM treating arguments as character sets; strip literal prefixes and suffixes with REGEXP_REPLACE.

SqlData Integrity

Code

snippet
WITH samples(url, filename) AS (
    VALUES
        ('https://status.example.com', 'metrics.csv'),
        ('https://api.example.com',    'orders.csv')
)
SELECT
    url,
    -- LTRIM treats 'https://' as an unordered char set {h,t,p,s,:,/}, eating "st" from "status"!
    LTRIM(url, 'https://') AS broken_ltrim,
    -- RTRIM treats '.csv' as {.,c,s,v}, eating "cs" from "metrics"!
    RTRIM(filename, '.csv') AS broken_rtrim,
    -- Literal prefix and suffix removal via anchored regex or starts_with/substr
    REGEXP_REPLACE(url, '^https://', '') AS safe_host,
    REGEXP_REPLACE(filename, '\.csv$', '') AS safe_stem
FROM samples;

Annotated execution — LTRIM And RTRIM Strip Any Set Character

The statement is SELECT, touching samples.

What the statements actually assemble: a CTE (WITH … AS) that names an intermediate result before the main query.

Putting it to use

The samples table is the working surface here.

Interactive Tools for This Topic

FAQ

Which tables appear in this script?
`samples`.