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);

insert image description here
Two vertical bars ( ||) can also be used to concatenate strings, but a NULL parameter will return NULL.

SELECT 'Post'||'greSQL', 'Post'||NULL||'greSQL';

insert image description here

Characters and Encodings

ascii(string)The function returns the code of the first character ASCII . For UTF8 return Unicodecodes; for other multibyte
encodings, the argument must be a ASCIIcharacter.

select ascii('x');

insert image description here
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
insert image description here

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.
insert image description here

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
insert image description here

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.
insert image description here
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
.
insert image description here
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.
insert image description here
regexp_match(string, pattern [, flags])The function returns the first substring that matches a POSIX regular expression.
insert image description here
regexp_matches(string, pattern [, flags])The function returns all substrings that match the POSIX regular expression, and the result
is a set that
insert image description here
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.
insert image description here
starts_with(string, prefix)The function judges whether the string prefixbegins with , and returns true if it is; otherwise, returns
false.
insert image description here
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
insert image description here
translate(string , FROM , to)
FROM
insert image description here
overlay(string placing substring FROM [for])
The for characters from which FROM begins.

select overlay('txxxxas' placing 'hom' from 2 for 4);

insert image description here

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');

insert image description here
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 函数的 trailingthe options are the same.

select btrim('yxTomxx', 'xyz'), ltrim('yxTomxx', 'xyz'), rtrim('yxTomxx',
'xyz');

insert image description here
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);

insert image description here

string formatting

format(formatstr , formatarg)Used to format strings, similar to the sprintf function in C language.

select format('hello %s, %1$s', 'world');

insert image description here

MD5 value

md5(string)Function used to return the MD5 value in hexadecimal format.
insert image description here

string split

regexp_split_to_table(string, pattern[, flags])Function to split a string, using POSIXa regular expression
as the delimiter. The return type of the function is a text collection.

select regexp_split_to_table('hello world', '\s+');

insert image description here
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);

insert image description here

string reverse

reverse(str)function to reverse a string.

select reverse('上海自来水');

insert image description here

Guess you like

Origin blog.csdn.net/Java_Fly1/article/details/132513517