postgresql - character functions
string concatenation
concat(str, …)
The function is used to concatenate strings, and ignores the NULL parameter; concat_ws(sep, str, …)
the function uses the specified separator sep to concatenate strings.
select concat(2, null, 22), concat_ws(' and ', 2, null, 22);
Two vertical bars ( ||
) can also be used to concatenate strings, but a NULL parameter will return NULL.
SELECT 'Post'||'greSQL', 'Post'||NULL||'greSQL';
Characters and Encodings
ascii(string)
The function returns the code of the first character ASCII
. For UTF8 return Unicode
codes; for other multibyte
encodings, the argument must be a ASCII
character.
select ascii('x');
chr(int)
The function returns the character corresponding to the encoding. For UTF8, the parameter specifies the Unicode code; for other multi-byte
encodings, the parameter must correspond to an ASCII character. The parameter is not allowed to be 0 (null character), because the text data type cannot store
a null character
string length
bit_length(string)
The function is used to calculate the number of bits contained in the string; length(string)
, char_length(string)
,
character_length(string)
the function calculates the number of characters contained in the string; octet_length(string)
the function calculates the number of bytes
contained in the string.
case conversion
lower(string)
Function converts a string to lowercase,
upper(string)
function converts a string to uppercase,
initcap(string)
function capitalizes the first letter of each word and lowercases the rest
Substring Find and Replace
substring(string [FROM] [for])
The function is used to extract the for character substring starting from the position FROM, and the position
is counted from 1. substr(string, FROM [, count])
have the same effect.
left(str, n)
The function returns n characters from the left of the string.
If n is negative, returns all but the last |n| characters
right(str, n)
The function returns the n characters to the right of the string. If n is negative, return characters excluding |n| characters to the left
.
substring(string FROM pattern)
The function extracts substrings that match a POSIX regular expression.
substring(string FROM pattern for escape)
Function extracts substrings that match a SQL regular expression.
regexp_match(string, pattern [, flags])
The function returns the first substring that matches a POSIX regular expression.
regexp_matches(string, pattern [, flags])
The function returns all substrings that match the POSIX regular expression, and the result
is a set that
position(substring in string)
returns the positions of the substrings; strpos(string, substring)
the function has the same effect, but
the order of the parameters is reversed.
starts_with(string, prefix)
The function judges whether the string prefix
begins with , and returns true if it is; otherwise, returns
false.
replace(string, FROM, to)
The function replaces the FROM substring in the string string with to
a substring; the function replaces the substring
regexp_replace(string, pattern, replacement [, flags])
in the string string that matches POSIX
the regular expression with . The function replaces the character string that appears in FROM in the string string with the character at the corresponding position in to. If the length is greater than to, characters that do not have a corresponding value in to will be deleted. The function uses substring to overwrite the string from
pattern
replacement
translate(string , FROM , to)
FROM
overlay(string placing substring FROM [for])
The for characters from which FROM begins.
select overlay('txxxxas' placing 'hom' from 2 for 4);
Truncate and fill
trim([leading | trailing | both] [characters] FROM string)
The function deletes the longest substring consisting of the specified characters (space by default) from the beginning (leading), end (trailing) or both ends (both) of the string;
trim([leading | trailing | both] [FROM] string [, characters])
the function has the same effect.
select trim(both 'xyz' from 'yxTomxx');
btrim(string [, characters])
The function of the function is the same as the both option of the trim function above; ltrim(string [, characters])
it is the same as the leading option of the trim function above; rtrim(string [, characters])函数上面 trim 函数的 trailing
the options are the same.
select btrim('yxTomxx', 'xyz'), ltrim('yxTomxx', 'xyz'), rtrim('yxTomxx',
'xyz');
lpad(string, length [, fill ])
The function fills the string with the characters in fill (default space) on the left until
the length is length. If the string is longer than length, it is truncated to length length from the right.
rpad(string, length [, fill ])
The function fills the string with the characters in fill (spaces by default) on the right side of the string until
the length is length. If the string is longer than length, it is truncated to length length from the right.
repeat(string, number)
The function repeats the string string N times.
select lpad('hi', 5, 'xy'), rpad('hi', 5, 'xy'), repeat('Pg', 4);
string formatting
format(formatstr , formatarg)
Used to format strings, similar to the sprintf function in C language.
select format('hello %s, %1$s', 'world');
MD5 value
md5(string)
Function used to return the MD5 value in hexadecimal format.
string split
regexp_split_to_table(string, pattern[, flags])
Function to split a string, using POSIX
a regular expression
as the delimiter. The return type of the function is a text collection.
select regexp_split_to_table('hello world', '\s+');
split_part(string, delimiter, field)
The function delimiter
splits the string and returns the specified item (
counting from 1).
select split_part('abc~@~def~@~ghi', '~@~', 2);
string reverse
reverse(str)
function to reverse a string.
select reverse('上海自来水');