Strings & regular expressions
These functions operate on VARCHAR values. Function names are
case-insensitive.
| Function | Result |
|---|---|
| contains | Whether text contains a substring. |
| length | Character count. |
| substring | A character slice. |
| regexp_full_match | Whether the entire string matches a pattern. |
| regexp_replace | Text with the first match replaced. |
| regexp_jit_replace | First-match replacement using PCRE2 JIT. |
contains
Section titled “contains”contains(string, substring)Returns a boolean indicating whether the string column contains the constant
substring.
SELECT contains(text, 'ivo') AS matchesFROM (VALUES ('Pivot'), ('Lake')) AS input(text);-- matches: true, falselength
Section titled “length”length(string)Returns the number of characters in the string. len and strlen are aliases.
SELECT length(text) AS charactersFROM (VALUES ('Pivot')) AS input(text);-- characters: 5substring
Section titled “substring”substring(string, start[, length])Returns a string slice, counting positions in characters. substr is an
alias. start and length must be non-negative integer constants. Omitting
length reads to the end of the string.
Positions start at 1. A start of 0 is also accepted: with an explicit
length, it consumes one character of that length before the beginning of the
string. Negative positions and lengths are not supported.
SELECT substring(text, 2, 3) AS pieceFROM (VALUES ('Pivot')) AS input(text);-- piece: ivoregexp_full_match
Section titled “regexp_full_match”regexp_full_match(string, pattern)Returns a boolean indicating whether the complete string matches the constant
pattern. The ~, !~, and SIMILAR TO forms use the same matcher.
SELECT regexp_full_match(text, '[a-z]+') AS matchesFROM (VALUES ('pivot'), ('pivot42')) AS input(text);-- matches: true, falseThe optional regex settings argument is not supported.
regexp_replace
Section titled “regexp_replace”regexp_replace(string, pattern, replacement)Returns text with the first matching substring replaced. Both pattern
and replacement must be constants.
SELECT regexp_replace(text, 'a', 'X') AS replacedFROM (VALUES ('banana')) AS input(text);-- replaced: bXnanaA fourth settings argument is not supported.
regexp_jit_replace
Section titled “regexp_jit_replace”regexp_jit_replace(string, pattern, replacement)Returns text with the first match replaced, using a pattern compiled by PCRE2 JIT. Pattern and replacement must be constants.
SELECT regexp_jit_replace(text, 'a', 'X') AS replacedFROM (VALUES ('banana')) AS input(text);-- replaced: bXnana