Contact us

The following are the built-in functions for working with character strings and byte sequences. They are divided into four sections:

  • Basic Functions - Functions for determining length, searching and comparing strings, etc.
  • Modifying Functions - Functions that modify character strings or byte arrays, such as UPPER, LPAD, BTRIM, or extract substrings, e.g. SUBSTRING, SPLIT_PART.
  • Parsing & Formatting Functions - Functions for parsing values from text/formatting values as text.
  • Binary Conversion Functions - Functions for converting between raw binary sequences and their character representations or other primitive types (integers, floating-points).

Basic Functions


fn CHAR_LENGTH
CHAR_LENGTH(str: text) -> int64

Returns the number of characters in a string.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

This number is not equal to the number of bytes when the string contains any non-ASCII characters.

Examples

SELECT CHAR_LENGTH('abcd');
  --> 4
SELECT CHAR_LENGTH('München');
  --> 7

See also


fn OCTET_LENGTH
OCTET_LENGTH(str: text) -> int64
OCTET_LENGTH(bytes: binary) -> int64

Returns the number of bytes in a string or byte sequence.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

This number is not equal to the number of characters when the character string contains any non-ASCII characters.

Examples

SELECT OCTET_LENGTH('abcd');
  --> 4
SELECT OCTET_LENGTH('München');
  --> 8
SELECT OCTET_LENGTH(x'f0ab17');
  --> 3

See also


fn LENGTH
LENGTH(str: text) -> int64
LENGTH(bytes: binary) -> int64

Returns the number of characters in a string or bytes in a byte sequence.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

When the argument is a character string, this function is actually an alias for CHAR_LENGTH.
When the argument is a byte sequence, the function is an alias for OCTET_LENGTH.

Examples

SELECT LENGTH('abcdxyz');
  --> 7
SELECT LENGTH(x'f0ab17');
  --> 3

fn STARTS_WITH
STARTS_WITH(str: text, prefix: text) -> bool
STARTS_WITH(bytes: binary, prefix: binary) -> bool

Returns true if a character string or a byte sequence starts with a given prefix, false otherwise.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Examples

SELECT STARTS_WITH('abracadabra', 'ab');
  --> true
SELECT STARTS_WITH('banana', 'ab');
  --> false
SELECT STARTS_WITH(x'ab0acadab1a0', x'ab');
  --> true
SELECT STARTS_WITH(x'ba4a4a', x'ab');
  --> false

fn ENDS_WITH
ENDS_WITH(str: text, suffix: text) -> bool
ENDS_WITH(bytes: binary, suffix: binary) -> bool

Returns true if a character string or a byte sequence ends with a given suffix, false otherwise.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Examples

SELECT ENDS_WITH('baobab', 'ab');
  --> true
SELECT ENDS_WITH('eucalyptus', 'ab');
  --> false
SELECT ENDS_WITH(x'ba0bab', x'ab');
  --> true
SELECT ENDS_WITH(x'ba0bab21', x'ab');
  --> false

fn POSITION
POSITION(substr: text, str: text) -> int64
POSITION(substr: text, str: text, startpos: int64) -> int64
POSITION(subseq: binary, bytes: binary) -> int64
POSITION(subseq: binary, bytes: binary, startpos: int64) -> int64

Returns the position of a given substring/subsequence within a string/byte sequence, optionally starting from a given position.

Arguments

Both the resulting position and the startpos argument are 1-based.

If the startpos argument is negative, it is counted from the end of the string/sequence.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Notes

If there are no occurrences of the given substring/subsequence in the string/sequence, the function will return 0.

If the startpos value is equal to 0 or larger than the length of the string/sequence, the function will just return 0.

Examples

SELECT POSITION('ab', 'abracadabra');
  --> 1
SELECT POSITION('ab', 'abracadabra', 4);
  --> 8
SELECT POSITION('ab', 'abacus', 4);
  --> 0
SELECT POSITION(x'ab', x'ab0acad0ab1a');
  --> 1
SELECT POSITION(x'ab', x'ab0acad0ab1a', 4);
  --> 5
SELECT POSITION(x'ab', x'abac79', 4);
  --> 0

See also


fn CHR
CHR(code: int64) -> text

Returns the character with a given Unicode code point value.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT CHR(65);
  --> 'A'
SELECT CHR(119);
  --> 'w'

See also


fn ORD
ORD(chr: text) -> int64

Returns the Unicode code point of the first character of the argument.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

If the argument is an empty string, the function will return 0.

Examples

SELECT ORD('A');
  --> 65
SELECT ORD('w');
  --> 119

See also


fn STRCMP
STRCMP(str1: text, str2: text) -> i64
STRCMP(bytes1: binary, bytes2: binary) -> i64

Returns -1 when the first string/byte sequence is lexicographically smaller than the second, 1 when it's larger, and 0 when they are equal.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Notes

This function is used intrinsically whenever two character strings/byte sequences are being compared.

Examples

SELECT STRCMP('abc', 'def');
  --> -1
SELECT STRCMP('abc', 'aaa');
  --> 1
SELECT STRCMP('abc', 'abc');
  --> 0
SELECT STRCMP(x'abcd', x'def1');
  --> -1
SELECT STRCMP(x'abcd', x'aaa1');
  --> 1
SELECT STRCMP(x'abcd', x'abcd');
  --> 0

String-modifying Functions


fn CONCAT
CONCAT(val1: T1, val2: T2, ...) -> text

Concatenates the textual representations of all the arguments.

Arguments

This function can take arbitrarily many arguments, but at least one argument is required.

All the arguments have to be convertible to a textual representation.

Null handling

If any of the arguments is NULL, it will be ignored.

Examples

SELECT CONCAT('a word', 151, true);
  --> 'a word151true'
SELECT CONCAT('another word', NULL::int64, false);
  --> 'another wordfalse'

fn CONCAT_WS
CONCAT_WS(delimiter: text, val1: T1, val2: T2, ...) -> text

Concatenates the textual representations of all but the first arguments, with the first argument as a delimiter.

Arguments

This function can take arbitrarily many arguments, but at least one argument besides the delimiter is required.

All the arguments have to be convertible to a textual representation.

Null handling

If any of the arguments is NULL, it will be ignored and the delimiter won't be placed in the result.

Notes

When the given delimiter is NULL or an empty string, this function behaves the same as the CONCAT function.

Examples

SELECT CONCAT_WS(', ', 'a word', 151, true);
  --> 'a word, 151, true'
SELECT CONCAT_WS(', ', 'another word', NULL::int64, false);
  --> 'another word, false'

See also


fn LOWER
LOWER(str: text) -> text

Converts the string to lowercase.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT LOWER('Some striNG');
  --> 'some string'

See also


fn UPPER
UPPER(str: text) -> text

Converts the string to uppercase.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT UPPER('Some striNG');
  --> 'SOME STRING'

See also


fn INITCAP
INITCAP(str: text) -> text

Convert the first letter of each word to upper case and the rest to lower case.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

In this context, words are considered to be sequences of alphanumeric characters separated by non-alphanumeric characters.

Examples

SELECT INITCAP('Some striNG');
  --> 'Some String'

See also


fn BTRIM
BTRIM(str: text) -> text
BTRIM(str: text, chars: text) -> text
BTRIM(bytes: binary) -> binary
BTRIM(bytes: binary, values: binary) -> binary

Removes the longest string/sequence consisting only of given characters/bytes (a space by default for text, zero for byte sequence) from the start and end of a string/byte sequence.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Examples

SELECT BTRIM('  a word   ');
  --> 'a word'
SELECT BTRIM('** a word#!', '*#!');
  --> ' a word'
SELECT BTRIM(x'00123a0000');
  --> x'123a'
SELECT BTRIM(x'010143facd0102', x'0102');
  --> x'43facd'

See also


fn LTRIM
LTRIM(str: text) -> text
LTRIM(str: text, chars: text) -> text
LTRIM(bytes: binary) -> binary
LTRIM(bytes: binary, values: binary) -> binary

Removes the longest string/sequence consisting only of given characters/bytes (a space by default for text, zero for byte sequence) from the start of a string/byte sequence.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Examples

SELECT LTRIM('  a word   ');
  --> 'a word   '
SELECT LTRIM('** a word#!', '*#!');
  --> ' a word#!'
SELECT LTRIM(x'00123a0000');
  --> x'123a0000'
SELECT LTRIM(x'010143facd0102', x'0102');
  --> x'43facd0102'

See also


fn RTRIM
RTRIM(str: text) -> text
RTRIM(str: text, chars: text) -> text
RTRIM(bytes: binary) -> binary
RTRIM(bytes: binary, values: binary) -> binary

Removes the longest string/sequence consisting only of given characters/bytes (a space by default for text, zero for byte sequence) from the end of a string/byte sequence.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Examples

SELECT RTRIM('  a word   ');
  --> '  a word'
SELECT RTRIM('** a word#!', '*#!');
  --> '** a word'
SELECT RTRIM(x'00123a0000');
  --> x'00123a'
SELECT RTRIM(x'010143facd0102', x'0102');
  --> x'010143facd'

See also


fn LPAD
LPAD(str: text, len: int64) -> text
LPAD(str: text, len: int64, fill: text) -> text
LPAD(bytes: binary, len: int64) -> binary
LPAD(bytes: binary, len: int64, fill: binary) -> binary

Fill up the string/byte sequence to given length by prepending the characters/bytes from fill (a space by default for text string, zero for byte sequence).

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the len argument is negative, the function will return an error: Function argument len must not be negative.

Notes

If the string/sequence is already longer than the given length, it will be truncated (on the right).

Examples

SELECT LPAD('a string', 13);
  --> '     a string'
SELECT LPAD('a very long string', 13);
  --> 'a very long s'
SELECT LPAD('a string', 13, '*.');
  --> '*.*.*a string'
SELECT LPAD(x'01020304', 7);
  --> x'00000001020304'
SELECT LPAD(x'010203040506070809010b0c', 8);
  --> x'0102030405060708'
SELECT LPAD(x'01020304', 7, x'aa');
  --> x'aaaaaa01020304'

See also


fn RPAD
RPAD(str: text, len: int64) -> text
RPAD(str: text, len: int64, fill: text) -> text
RPAD(bytes: binary, len: int64) -> binary
RPAD(bytes: binary, len: int64, fill: binary) -> binary

Fill up the string/byte sequence to given length by appending the characters/bytes from fill (a space by default for text string, zero for byte sequence).

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the len argument is negative, the function will return an error: Function argument len must not be negative.

Notes

If the string/sequence is already longer than the given length, it will be truncated (on the right).

Examples

SELECT RPAD('a string', 13);
  --> 'a string     '
SELECT RPAD('a very long string', 13);
  --> 'a very long s'
SELECT RPAD('a string', 13, '*.');
  --> 'a string*.*.*'
SELECT RPAD(x'01020304', 7);
  --> x'01020304000000'
SELECT RPAD(x'010203040506070809010b0c', 8);
  --> x'0102030405060708'
SELECT RPAD(x'01020304', 7, x'aa');
  --> x'01020304aaaaaa'

See also


fn REPEAT
REPEAT(str: text, num: int64) -> text
REPEAT(bytes: binary, num: int64) -> binary

Repeats the string/byte sequence the specified number of times.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the num argument is negative, the function will return an error: Function argument num must not be negative.

Examples

SELECT REPEAT('ABcd', 3);
  --> 'ABcdABcdABcd'
SELECT REPEAT(x'ab12', 3);
  --> x'ab12ab12ab12'

fn REPLACE
REPLACE(str: text, from: text, to: text) -> text
REPLACE(bytes: binary, from: binary, to: binary) -> binary

Returns the given string/byte sequence with each occurrence of a substring/subsequence from replaced with a substring/subsequence to.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Notes

If the from argument is an empty string/sequence, the function will return the input unchanged.

Examples

SELECT REPLACE('abacus', 'ab', 'xyz');
  --> 'xyzacus'
SELECT REPLACE('abracadabra', 'ab', 'xyz');
  --> 'xyzracadxyzra'
SELECT REPLACE(x'aba123', x'ab', x'4560');
  --> x'4560a123'
SELECT REPLACE(x'ab1acad0ab1a', x'ab', x'4560');
  --> x'45601acad045601a'

See also


fn REVERSE
REVERSE(str: text) -> text
REVERSE(bytes: binary) -> binary

Returns the given string/byte sequence with the order of the characters/bytes reversed.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT REVERSE('some string');
  --> 'gnirts emos'
SELECT REVERSE(REVERSE('some string'));
  --> 'some string'
SELECT REVERSE(x'12340abcde');
  --> x'debc0a3412'
SELECT REVERSE(REVERSE(x'12340abcde'));
  --> x'12340abcde'

fn SUBSTRING
SUBSTRING(str: text, pos: int64) -> text
SUBSTRING(str: text, pos: int64, len: int64) -> text
SUBSTRING(bytes: binary, pos: int64) -> binary
SUBSTRING(bytes: binary, pos: int64, len: int64) -> binary

Returns the substring of a string (or a byte sequence) starting at the given position, optionally with length of up to len characters/bytes.

Arguments

The pos argument is 1-based. If it's negative, the position is counted from the end of the string/sequence.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the len argument is negative, the function will return an error: Function argument len must not be negative

Notes

If the value of the pos is equal to 0 or larger than the length of the string/sequence, the function will return an empty string/sequence.

Examples

SELECT SUBSTRING('abracadabra', 4);
  --> 'acadabra'
SELECT SUBSTRING('abracadabra', -4, 2);
  --> 'ab'
SELECT SUBSTRING(x'ab1acad0ab1a', 4);
  --> x'd0ab1a'
SELECT SUBSTRING(x'ab1acad0ab1a', -4, 2);
  --> x'cad0'

fn LEFT
LEFT(str: text, num: int64) -> text
LEFT(bytes: binary, num: int64) -> binary

Returns the leftmost num characters of a string or bytes of a byte sequence.

Arguments

If num is negative, the function returns all but the last num characters/bytes.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Notes

When the value of num is larger than the length of the string/sequence, the whole string/sequence will be returned.
When the value of num is negative and larger than length, an empty string/sequence will be returned.

Examples

SELECT LEFT('a sentence of words', 4);
  --> 'a se'
SELECT LEFT('abc', 4);
  --> 'abc'
SELECT LEFT('a sentence of words', -4);
  --> 'a sentence of w'
SELECT LEFT(x'0102030405060708', 4);
  --> x'01020304'
SELECT LEFT(x'010203', 4);
  --> x'010203'
SELECT LEFT(x'010203040506070809', -4);
  --> x'0102030405'

See also


fn RIGHT
RIGHT(str: text, num: int64) -> text
RIGHT(bytes: binary, num: int64) -> binary

Returns the rightmost num characters of a string or bytes of a byte sequence.

Arguments

If num is negative, the function returns all but the first num characters/bytes.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Notes

When the value of num is larger than the length of the string/sequence, the whole string/sequence is returned.
When the value of num is negative and larger than length, an empty string/sequence is returned.

Examples

SELECT RIGHT('a sentence of words', 4);
  --> 'ords'
SELECT RIGHT('abc', 4);
  --> 'abc'
SELECT RIGHT('a sentence of words', -4);
  --> 'ntence of words'
SELECT RIGHT(x'0102030405060708', 4);
  --> x'05060708'
SELECT RIGHT(x'010203', 4);
  --> x'010203'
SELECT RIGHT(x'010203040506070809', -4);
  --> x'0506070809'

See also


fn SPLIT_PART
SPLIT_PART(str: text, delimiter: text, part: int64) -> text

Splits the given string on the delimiter and returns the part-th part.

Arguments

The part argument is 1-based.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the part argument is negative, the function will return an error: Function argument part must not be negative.

Notes

If there are fewer than part parts, the function will return an empty string.

Examples

SELECT SPLIT_PART('abc;123;def;456', ';', 3);
  --> 'def'
SELECT SPLIT_PART('xyz;789', ';', 3);
  --> ''

See also


fn JSON_ESCAPE
JSON_ESCAPE(str: text) -> text

Returns the given string with all JSON special characters escaped.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

The JSON special characters that will be escaped are:

Original character(s) Escaped character(s)
Double quote " \"
Backslash \ \\
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t
Characters 0x00-0x1F \u0000-\u001F

Examples

SELECT JSON_ESCAPE('""');
  --> '\"\"'

fn TRANSLATE
TRANSLATE(str: text, from: text, to: text) -> text

Replaces each occurrence of any character of from in the given string with the corresponding character from to.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If the lengths of the from and to arguments are not the same, the function will return an error: Function arguments to and from must have the same length.

Examples

SELECT TRANSLATE('abracadabra', 'abc', 'xyz');
  --> 'xyrxzxdxyrx'

See also


Parsing & Formatting Functions


fn FORMAT
FORMAT(formatstr: text, val1: T1, val2: T2, ...) -> text

Formats the given arguments according to a format string.

Arguments

This function takes an arbitrary number of arguments. Only the formatstr argument is required, others are optional.

Null handling

If any of the arguments is NULL, the function will also return NULL.

Error handling

If an unsupported format specifier is given, the function will return an error: Unknown format specifier.

If a format specifier doesn't match the type of its corresponding argument (e.g. FORMAT('%f', true)), the function will return an error: Argument doesn't match the type specifier.

Notes

The format string consists of ordinary characters (which are copied verbatim to the output) and format specifiers.
The following is a list of all the available format specifiers:

Specifier Meaning Example
%t Boolean value true, false
%d Integral value, decimal 123, -45
%b Integral value, binary 10010, 11101
%o Integral value, octal 154, 75712
%x Integral value, hexadecimal ffff13, 14a
%f Floating-point value 1.45, -2.67
%s Character string value hello, world
%% A percent sign %

Examples

SELECT FORMAT('a=%t, b=%d, c=%f, d=%s', true, 37, 4.51, 'a string');
  --> 'a=true, b=37, c=4.51, d=a string'
SELECT FORMAT('%d, %x, %o, %b', 255, 255, 255, 255);
  --> '255, ff, 377, 11111111'

fn TO_CHAR
TO_CHAR(x: bool) -> text
TO_CHAR(x: int64) -> text
TO_CHAR(x: float64) -> text

Converts a given argument to its textual representation.

Null handling

If the argument is NULL, the function will also return NULL.

Notes

There is also a specialized TO_CHAR (temporal) function for converting timestamp and timeperiod values.

Examples

SELECT TO_CHAR(true);
  --> 'true'
SELECT TO_CHAR(32);
  --> '32'
SELECT TO_CHAR(-13.45);
  --> '-13.45'

fn PARSE_BOOL
PARSE_BOOL(str: text) -> bool

Parses a boolean value from a given string.

Null handling

If the argument is NULL, the function will also return NULL.

Error handling

If a boolean value cannot be parsed from a given string, the function will return an error: Cannot parse a boolean value from string.

Notes

The function expects the string to be either 'true' or 'false' (case-insensitively).

Any whitespace at the start or the end of the string is ignored.

Examples

SELECT PARSE_BOOL('TRUE');
  --> true

fn PARSE_FLOAT
PARSE_FLOAT(str: text) -> float64

Parses a floating-point value from a given string.

Null handling

If the argument is NULL, the function will also return NULL.

Error handling

If a floating-point value cannot be parsed from a given string, the function will return an error: Cannot parse a floating-point value from string.

If the parsed value doesn't fit into the 64-bit floating-point number, the function will return an error: Result out of representable range.

Notes

The function expects the string to be in the decimal format (i.e. to have a pattern (+|-)[0-9]+\.?[0-9]*).

Any whitespace at the start or the end of the string is ignored.

Examples

SELECT PARSE_FLOAT('3.14');
  --> 3.14

fn PARSE_INT
PARSE_INT(str: text) -> int64

Parses an integral value from a given string.

Null handling

If the argument is NULL, the function will also return NULL.

Error handling

If an integral value cannot be parsed from a given string, the function will return an error: Cannot parse an integer value from string.

If the parsed value doesn't fit into the 64-bit integer, the function will return an error: Result out of representable range.

Notes

The function expects the string to be to be in the decimal format (i.e. to have a pattern (+|-)[0-9]+).

Any whitespace at the start or the end of the string is ignored.

Examples

SELECT PARSE_INT('  12');
  --> 12

Binary Conversion Functions


fn HEX_ENCODE
HEX_ENCODE(bytes: binary) -> text

Returns a hexadecimal representation of a given byte sequence.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT HEX_ENCODE(x'012345abcd');
  --> '012345abcd'

fn HEX_DECODE
HEX_DECODE(hex_str: text) -> binary

Decodes a byte sequence from its hex-encoded string representation.

Null handling

If the argument is NULL, the function will also return NULL.

Error handling

If a given string is not a valid hex-encoded byte sequence (e.g. it's length is odd or it contains characters which are not valid hex digits), the function will return an error: Invalid hex string.

Examples

SELECT HEX_DECODE('012345abcd');
  --> x'012345abcd'

fn BASE64_ENCODE
BASE64_ENCODE(bytes: binary) -> text

Returns a base64 representation of a given byte sequence.

Null handling

If the argument is NULL, the function will also return NULL.

Examples

SELECT BASE64_ENCODE(x'0102030405060708090a');
  --> 'AQIDBAUGBwgJCg=='

fn BASE64_DECODE
BASE64_DECODE(b64_str: text) -> binary

Decodes a byte sequence from its base64-encoded string representation.

Null handling

If the argument is NULL, the function will also return NULL.

Error handling

If the given string is not a valid base64-encoded byte sequence (e.g. it's length is not a multiple of 4, it contains characters which are not valid base64 symbols or it has an incorrect ending), the function will return an error: Invalid base64 string.

Examples

SELECT BASE64_DECODE('AQIDBAUGBwgJCg==');
  --> x'0102030405060708090a'