Skip to main content

String functions and operators

String operators

OperatorExpression & DescriptionExample
||expression1 || expression2 [ || expression ] ...
Concatenates two or more expressions.
'Abcde' || 1 || 23 Abcde123
^@string ^@ substring
Returns true (t) if string starts with substring. This operator is equivalent to the starts_with() function.
'abcdef' ^@ 'abc't

String functions

FunctionDescriptionExample
ascii ( input_string ) → intReturns the Unicode code point of the first character of the input_string. If the string is empty, it returns NULL.ascii('RisingWave')82
ascii('🌊')127754
bit_length ( input_string ) → integerReturns number of bits in the input string, which is 8 times the octet_length.bit_length('wave')32
btrim ( input_string [, characters ]) → output_stringEquals to trim (BOTH).btrim(' cake ') → 'cake'
btrim('abcxyzabc', 'cba')xyz
char_length ( input_string ) → integer_output
character_length ( input_string ) → integer_output
length ( input_string ) → integer_output
Returns the number of characters in input_string.char_length('wave')4
chr ( input_int ) → stringReturns the character with the Unicode code point equivalent to the input_int value provided.chr(65)A
concat ( any_input-value_1 [, any_input-value_2 [, ...] ]) → output_stringConcatenates the arguments. NULL arguments are ignored.concat('Abcde', 2, NULL, 22)Abcde222
concat_ws ( separator_string, any_input-value_1 [, any_input-value_2 [, ...] ]) → output_stringConcatenates the arguments with a separator. The first argument is used as the separator, and should not be NULL. Other NULL arguments are ignored.concat_ws(',', 'Abcde', 2, NULL, 22)Abcde,2,22
decode(input_string, format_type) → byteaDecodes the text data in input_string into binary data. Supported formats for the encoded input_string include base64, hex, and escape.decode('MTIz', 'base64')123
encode(bytea, format_type) → output_stringEncodes the binary data in bytea into its textual representation. Supported encoding formats include base64, hex, and escape.encode(E'123'::bytea, 'base64')MTIz
initcap( input_string ) → stringCapitalizes the first letter of each word in the input_string and converts the remaining characters to lowercase.initcap('POWERFUL and flexible')Powerful And Flexible
lower ( input_string ) → output_stringConverts the string to all lowercase.lower('TOM')tom
lpad( input_string, input_int ) → string

lpad( input_string, input_int, padding_string ) → string
Pads the input_string on the left with spaces until it reaches the specified input_int length. If the input_string is longer than the input_int length, it is truncated to the specified length. Providing the optional padding_string replaces the spaces with the padding_string.lpad('42', 5) → '   42'

lpad('42', 5, 'R')RRR42
ltrim ( input_string [, characters ]) → output_stringEquals to trim (LEADING).ltrim(' cake ') → 'cake '
ltrim('abcxyzabc', 'cba')xyzabc
octet_length ( *input_string* )Returns number of bytes in the string.octet_length('wave')4
overlay ( input_string PLACING substring FROM start_int [ FOR length_int ]) → output_stringReplaces a substring in input_string with substring, starting at start_int and with length_int. If length_int is omitted, its value is the length of substring.overlay('yabadoo' PLACING 'daba' FROM 5 FOR 0)yabadabadoo
overlay('abcdef' PLACING '45' FROM 4)abc45f
overlay('RisingWave' PLACING '🌊' FROM 7)Rising🌊ave
position ( substring in input_string ) → integer_outputReturns the starting index of the specified substring within input_string, or zero if it is not present.position('ing' in 'rising')4
rtrim ( input_string [, characters ]) → output_stringEquals to trim (TRAILING).rtrim(' cake ') → ' cake'
rtrim('abcxyzabc', 'cba')abcxyz
regexp_match ( input_string, pattern, [, optional_flag ] ) → matched_string []Returns a string array of captured substring(s) resulting from the first match of a POSIX regular expression pattern to a string. If there is no match, the result is NULL. Optional flags include i, which stands for case-insensitive matching, and c, which represents case-sensitive matching.regexp_match('foobarbequebaz', '(bar)(beque)'){bar,beque}
regexp_match('abc', 'd')NULL
regexp_match('abc', 'Bc', 'ici'){bc}
regexp_matches ( input_string, pattern, [, optional_flag ] ) → set_of_matched_string []Returns a set of string arrays of captured substring(s) resulting from matching a POSIX regular expression pattern to a string. Returns all matches by default. Optional flags include i, which stands for case-insensitive matching, and c, which represents case-sensitive matching.regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)')
{bar,beque}
{bazil,barf}
regexp_matches('abcabc', 'Bc', 'i')
{bc}
{bc}
repeat ( input_string, times_int ) → output_stringRepeats input_string specific times. Null is returned when times_int is zero, negative, or null.repeat('A1b2', 3)A1b2A1b2A1b2
replace ( input_string, from_string, to_string ) → output_stringReplaces all occurrences of substring from_string in input_string with substring to_string.replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
reverse( input_string ) → stringReturns the input_string with its characters in the reverse order.reverse('RisingWave')evaWgnisiR
rpad( input_string, input_int ) → string

rpad( input_string, input_int, padding_string ) → string
Pads the input_string on the right with spaces until it reaches the specified input_int length. If the input_string is longer than the input_int length, it is truncated to the specified length. Providing the optional padding_string replaces the spaces with the padding_string.rpad('42', 5) → '42   '

rpad('42', 5, 'R')42RRR
split_part ( input_string , delimiter_string, int_n ) → varcharSplits input_string at occurrences of delimiter_string and returns the int_n'th field (counting from one), or when int_n is negative, returns the |int_n|'th-from-last field. When int_n is zero, returns an 'InvalidParameterValue' error. When the input delimiter_string is an empty string, returns the input_string if querying the first or last field. Otherwise, returns an empty string.split_part('abc~@~def~@~ghi', '~@~', 2)def
starts_with( input_string, prefix_string ) → booleanReturns true if the input_string starts with the specified prefix_string, otherwise returns false.starts_with('RisingWave is powerful', 'Rising')true
starts_with( input_string, substring ) → booleanReturns true (t) if input_string starts with substring. This function is equivalent to the ^@ operator.starts_with('abcdef', 'abc')t
strpos( input_string, substring ) → intReturns the position of the first occurrence of the substring in the input_string. If the substring is not found, it returns 0.strpos('RisingWave is powerful', 'powerful')15
substr/substring ( input_string , start_int [, count_int ]) → output_stringExtracts the substring from input_string starting at position start_int and extending for count_int characters, if specified. start_int should be equal to or larger than 1.substr('alphabet', 3)phabet;
substring('alphabet', 3, 2)ph
to_ascii( input_string ) → stringReturns the input_string with non-ASCII characters replaced by their closest ASCII equivalents.to_ascii('Café')Cafe
to_hex( input_int ) → string

to_hex( input_bigint ) → string
Converts input_int or input_bigint to its hexadecimal representation as a string.to_hex(255)ff

to_hex(123456789012345678)1b69b4ba630f34e
translate ( input_string, from_string, to_string) → output_stringReplaces each character in the input_string that matches a character in the from_string with the corresponding character in the to_string.translate('M1X3', '13', 'ae')MaXe
trim ( [ LEADING | TRAILING | BOTH ] [ characters ] FROM input_string ) → output_stringTrims the longest contiguous substring of characters from the beginning, end, or both ends (BOTH by default) of input_string that contains only the characters specified in characters (which defaults to whitespace if not specified).trim(' cake ') → 'cake'
trim(both 'cba' from 'abcxyzabc')xyz
trim ( [ LEADING | TRAILING | BOTH ][ FROM ] input_string [, characters ] ) → output_stringAn alternative syntax of trim().trim(both from 'abcxyzabc', 'cba')xyz
trim('abcxyzabc', 'cba')xyz
upper ( input_string ) → output_stringConverts the string to all uppercase.upper('tom')TOM

Pattern matching expressions

string NOT LIKE pattern [ ESCAPE '' ]
string LIKE pattern [ ESCAPE '' ]

The LIKE expression returns true if the string matches the supplied pattern. The NOT LIKE expression returns false if LIKE returns true.

Wildcards

  • An underscore _ in a pattern matches any single character
  • A percent sign % matches any sequence of zero or more characters.

If the pattern does not contain _ or %, then the pattern only represents the string itself. For example, the pattern 'apple' matches only the string 'apple'. In that case, LIKE acts like the equals operator =.

Escape

To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character \. To match the escape character itself, write two escape characters: \\.

You can use ESCAPE '' to disable the escape mechanism, but specifying a custom escape character using the ESCAPE clause is not supported.

Examples

'abc' LIKE 'abc'           true
'abc' LIKE 'a%' true
'abc' LIKE '_b_' true
'abc' LIKE 'c' false

Help us make this doc better!

Was this page helpful?

Happy React is loading...