String Functions¶
UPPER(str)¶
Converts a string to uppercase.
LOWER(str)¶
Converts a string to lowercase.
LENGTH(str)¶
Returns the byte length of the string.
TRIM([str [, chars]])¶
Removes leading and trailing characters from a string. Defaults to whitespace.
SELECT TRIM(' hello '); -- 'hello'
SELECT TRIM('xxhelloxx', 'x'); -- 'hello'
SELECT TRIM(name) FROM users;
LTRIM([str [, chars]])¶
Removes leading characters only.
RTRIM([str [, chars]])¶
Removes trailing characters only.
SUBSTR(str, start [, length])¶
Returns a substring. start is 1-based. If length is omitted, returns to end of string.
SELECT SUBSTR('hello world', 7); -- 'world'
SELECT SUBSTR('hello world', 1, 5); -- 'hello'
SELECT SUBSTR(body, 1, 100) FROM articles;
REPLACE(str, from, to)¶
Replaces all occurrences of from with to.
SELECT REPLACE('foo bar foo', 'foo', 'baz'); -- 'baz bar baz'
SELECT REPLACE(email, '@old.com', '@new.com') FROM users;
CONCAT(str1, str2, ...)¶
Concatenates strings. NULL arguments are silently skipped (PostgreSQL semantics).
SELECT CONCAT('hello', ' ', 'world'); -- 'hello world'
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
Alternatively, use the || operator:
NULL behaviour¶
All string functions return NULL if any non-skippable argument is NULL:
CONCAT is an exception — it skips NULL arguments and returns the concatenation of non-null values.
Expression index example¶
String functions can be used in expression indexes to accelerate function-based predicates: