This section describes functions and operators for examining and manipulating string values. Strings in this context include values of the types character, character varying, and text. Except where noted, these functions and operators are declared to accept and return type text. They will interchangeably accept character varying arguments. Values of type character will be converted to text before the function or operator is applied, resulting in stripping any trailing spaces in the character value.
SQL defines some string functions that use key words, rather than commas, to separate arguments. Details are in Table 9.9. PostgreSQL also provides versions of these functions that use the regular function invocation syntax (see Table 9.10).
The string concatenation operator (||) will accept non-string input, so long as at least one input is of string type, as shown in Table 9.9. For other cases, inserting an explicit coercion to text can be used to have non-string input accepted.
Table 9.9. SQL String Functions and Operators
|
Function/Operator Description Example(s) |
|---|
|
Concatenates the two strings.
|
|
Converts the non-string input to text, then concatenates the two strings. (The non-string input cannot be of an array type, because that would create ambiguity with the array
|
|
Removes the longest string containing only characters in
|
|
Checks whether the string is in the specified Unicode normalization form. The optional
|
|
Returns number of bits in the string (8 times the
|
|
Returns number of characters in the string.
|
|
Converts the string to all lower case, according to the rules of the database's locale.
|
|
Extends the
|
|
Removes the longest string containing only characters in
|
|
Converts the string to the specified Unicode normalization form. The optional
|
|
Returns number of bytes in the string.
|
|
Returns number of bytes in the string. Since this version of the function accepts type
|
|
Replaces the substring of
|
|
Returns first starting index of the specified
|
|
Extends the
|
|
Removes the longest string containing only characters in
|
|
Extracts the substring of
|
|
Extracts the first substring matching POSIX regular expression; see Section 9.7.3.
|
|
Extracts the first substring matching SQL regular expression; see Section 9.7.2. The first form has been specified since SQL:2003; the second form was only in SQL:1999 and should be considered obsolete.
|
|
Removes the longest string containing only characters in
|
|
This is a non-standard syntax for
|
|
Returns |
|
Converts the string to all upper case, according to the rules of the database's locale.
|
Additional string manipulation functions and operators are available and are listed in Table 9.10. (Some of these are used internally to implement the SQL-standard string functions listed in Table 9.9.) There are also pattern-matching operators, which are described in Section 9.7, and operators for full-text search, which are described in Chapter 12.
Table 9.10. Other String Functions and Operators
|
Function/Operator Description Example(s) |
|---|
|
Returns true if the first string starts with the second string (equivalent to the
|
|
Returns the numeric code of the first character of the argument. In UTF8 encoding, returns the Unicode code point of the character. In other multibyte encodings, the argument must be an ASCII character.
|
|
Returns the character with the given code. In UTF8 encoding the argument is treated as a Unicode code point. In other multibyte encodings the argument must designate an ASCII character.
|
|
Concatenates the text representations of all the arguments. NULL arguments are ignored.
|
|
Concatenates all but the first argument, with separators. The first argument is used as the separator string, and should not be NULL. Other NULL arguments are ignored.
|
|
Formats arguments according to a format string; see Section 9.4.1. This function is similar to the C function
|
|
Converts the first letter of each word to upper case and the rest to lower case. Words are sequences of alphanumeric characters separated by non-alphanumeric characters.
|
|
Returns first
|
|
Returns the number of characters in the string.
|
|
Computes the MD5 hash of the argument, with the result written in hexadecimal.
|
|
Splits
|
|
Returns current client encoding name.
|
|
Returns the given string suitably quoted to be used as an identifier in an SQL statement string. Quotes are added only if necessary (i.e., if the string contains non-identifier characters or would be case-folded). Embedded quotes are properly doubled. See also Example 41.1.
|
|
Returns the given string suitably quoted to be used as a string literal in an SQL statement string. Embedded single-quotes and backslashes are properly doubled. Note that
|
|
Converts the given value to text and then quotes it as a literal. Embedded single-quotes and backslashes are properly doubled.
|
|
Returns the given string suitably quoted to be used as a string literal in an SQL statement string; or, if the argument is null, returns
|
|
Converts the given value to text and then quotes it as a literal; or, if the argument is null, returns
|
|
Returns the number of times the POSIX regular expression
|
|
Returns the position within
|
|
Checks whether a match of the POSIX regular expression
|
|
Returns substrings within the first match of the POSIX regular expression
|
|
Returns substrings within the first match of the POSIX regular expression
{bar}
{baz}
|
|
Replaces the substring that is the first match to the POSIX regular expression
|
|
Replaces the substring that is the
|
|
Splits
|
|
Splits
hello world |
|
Returns the substring within
|
|
Repeats
|
|
Replaces all occurrences in
|
|
Reverses the order of the characters in the string.
|
|
Returns last
|
|
Splits
|
|
Returns true if
|
|
|