This topic describes the functions and operators that are used to check and manipulate string values.
Strings in this context include values of the character, character varying, and text data types. Unless otherwise noted, these functions and operators are declared to accept and return values of the text data type. The functions and operators interchangeably accept values of the character varying data type. Before a function or operator is applied, values of the character data type are converted to the text data type. As a result, any trailing spaces in values of the character type are deleted.
SQL defines some string functions that use keywords other than commas (,) to separate parameters. For more information, see the "SQL string functions and operators" section of this topic. PolarDB for PostgreSQL(Compatible with Oracle) also provides versions of these functions that can be invoked by using the regular function invocation syntax. For more information, see the "Other string functions" section of this topic.
Due to the implicit type conversion of values from other data types to the text data type, the functions in the earlier versions of PolarDB for PostgreSQL(Compatible with Oracle) accept values of non-string types. The implicit type conversions are disabled in the current version of PolarDB for PostgreSQL(Compatible with Oracle). This is because these type conversions always cause surprising behaviors. However, the string concatenation operator (||) still accepts input values of non-string types, as long as at least one of the input values is of the string type. For more information, see the "SQL string functions and operators" section of this topic. In other cases, if you want to retain the previous behaviors, you can use an explicit type conversion to convert values to the text data type.
SQL string functions and operators
Concatenates two strings.
|
Converts non-string input to text and concatenates two strings. The non-string input cannot be of the array data type because this causes ambiguity with the array concatenation operator
|
Checks whether a string is in the specified Unicode normalization form.
|
Returns the number of bits in a string, which is eight times the return value of
|
Returns the number of characters in a string.
|
Converts all letters in a string to lowercase letters based on the locale of the database.
|
Converts a string to the specified Unicode normalization form.
|
Returns the number of bytes in a string.
|
Returns the number of bytes in a string. This version of the function accepts values of the
|
Replaces the substring from the character at the
|
Returns the start index of the specified
|
Extracts the substring from the character at the
|
Extracts the first substring that matches a POSIX regular expression.
|
Extracts the first substring that matches an SQL regular expression.
|
Removes the longest string that contains only the characters specified by
|
This is the non-standard syntax of
|
Convert all letters in a string to uppercase letters based on the locale of the database.
|
The following section describes other string functions that are available. Some of the functions are internally used to implement standard SQL string functions that are described in the previous table.
Other string functions
Returns the numeric code of the first character of the parameter. In UTF8 encoding, the Unicode code point of the character is returned. In other multi-byte encodings, the parameter must be an ASCII character.
|
Removes the longest string that contains only the characters specified by
|
Returns the character with the specified code. In UTF8 encoding, the parameter is parsed as a Unicode code point. In other multi-byte encodings, the parameter must specify an ASCII character.
|
Concatenates the text representations of all parameters. NULL parameters are ignored.
|
Concatenate all but the first parameter by using a delimiter. The first parameter specifies the delimiter and cannot be NULL. Other NULL parameters are ignored.
|
Formats parameters based on a format string. This function is similar to the C function
|
Converts the first letter of each word to an uppercase letter and the rest letters to lowercase letters. Words are sequences of alphanumeric characters separated with non-alphanumeric characters.
|
Returns the first
|
Returns the number of characters in a string.
|
Extends the length of the
|
Removes the longest string that contains only the characters specified by
|
Calculates the MD5 hash value of the parameter. The result is returned in the hexadecimal format.
|
Splits the string specified by
|
Returns the encoding name of the current client.
|
Returns a string properly quoted so that it can be used as an identifier in an SQL statement string. Quotation marks are added only if necessary. For example, a string is quoted if it contains non-identifier characters or is to be case folded. Embedded quotation marks are properly doubled.
|
Returns a string properly quoted so that it can be used as a string literal in an SQL statement string. Embedded single quotation marks (') and backslashes (/) are properly doubled. Note:
|
Converts the specified value to text and then quotes it as a string literal. Embedded single quotation marks (') and backslashes (/) are properly doubled.
|
Returns a string properly quoted so that it can be used as a string literal in an SQL statement string. If the parameter is NULL, the string literal
|
Converts the specified value to text and then quotes it as a string literal. If the parameter is NULL, the string literal
|
Returns the first substring in the
|
Returns the substrings in the
|
Replaces the substrings that match a POSIX regular expression.
|
Splits the
|
Splits the
|
Repeats the
|
Replaces all occurrences of the
|
Reverses the order of the characters in a string.
|
Returns the last
|
Extends the length of the
|
Removes the longest string that contains only the characters specified by
|
Splits the
|
Returns the start index of the specified
|
Extracts the substring from the character at the
|
Returns true if the
|
Converts the
|
Converts a number to a string in the hexadecimal format.
|
Replaces each character in the
|
The concat, concat_ws, and format functions are mutable. Therefore, you can pass the values to be concatenated or formatted as an array by using the VARIADIC keyword. The elements in the array are considered separate parameters of the function. If the array is NULL, concat and concat_ws return NULL, but format considers NULL as an array with zero elements.
format
The format function produces formatted output based on a format string. This function works in a way similar to the C function sprintf.
format(formatstr text [, formatarg "any" [, ...] ])formatstr is a format string that specifies how the result is formatted. The text in the format string is directly copied into the result, except the place in which a format specifier is used. A format specifier acts as a placeholder in the format string. A format specifier defines how subsequent function parameters are formatted and inserted into the result. Each formatarg parameter is converted to text based on the regular output rules of the data type of the parameter value, and then formatted and inserted into the result string based on the format specifier.
A format specifier starts with a percent sign (%) and is in the following format:
%[position][flags][width]typeDescription of the fields in a format specifier:
position: optional. The string in the n''$ format, in which n is the index of the parameter to be displayed. Index 1 indicates the first parameter after formatstr. If position is not specified, the next parameter in the sequence is used by default.
flags: optional. The additional option that controls how the output of the format specifier is formatted. Only the minus sign (-) is supported. The - flag specifies that the output of the format specifier is left-justified. This field does not take effect unless the width field is also specified.
width: optional. The minimum number of characters used to display the output of the format specifier. The output is padded with spaces on the left or right based on whether the - flag is specified. A small width does not cause the output to be truncated, but is simply ignored. The width can be specified by using one of the following formats: a positive integer, an asterisk (*) indicating that the next function parameter is used as the width, and a string in the *n$ format indicating that the nth function parameter is used as the width.
If the width comes from a function parameter, the parameter is consumed before it is used by the value of the format specifier. If the width is negative, the result is left-justified by using a width of abs(width). This is the same as the effect when the - flag is specified.
type: required. The type of format conversion that is used to generate the output of the format specifier. Valid values:
s: formats the parameter value as a simple string. A NULL value is considered an empty string.I: considers the parameter value as an SQL identifier and adds double quotation marks (") to the SQL identifier if necessary. If the parameter value is NULL, an error is returned, which is the same as the return of thequote_identfunction.L: quotes the parameter value as an SQL literal. A NULL value will be displayed as an unquoted stringNULL, which is the same as the return of thequote_nullablefunction.
In addition to the preceding format specifiers, you can use the special sequence %% to display a literal % character.
The following examples show basic format conversions:
SELECT format('Hello %s', 'World');
Result: Hello World
SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three');
Result: Testing one, two, three, %
SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly');
Result: INSERT INTO "Foo bar" VALUES('O''Reilly')
SELECT format('INSERT INTO %I VALUES(%L)', 'locations', 'C:\Program Files');
Result: INSERT INTO locations VALUES(E'C:\\Program Files') The following examples show how the width field and the - flag are used:
SELECT format('|%10s|', 'foo');
Result: | foo|
SELECT format('|%-10s|', 'foo');
Result: |foo |
SELECT format('|%*s|', 10, 'foo');
Result: | foo|
SELECT format('|%*s|', -10, 'foo');
Result: |foo |
SELECT format('|%-*s|', 10, 'foo');
Result: |foo |
SELECT format('|%-*s|', -10, 'foo');
Result: |foo |The following examples show how the position field is used:
SELECT format('Testing %3$s, %2$s, %1$s', 'one', 'two', 'three');
Result: Testing three, two, one
SELECT format('|%*2$s|', 'foo', 10, 'bar');
Result: | bar|
SELECT format('|%1$*2$s|', 'foo', 10, 'bar');
Result: | foo|Different from the standard C function sprintf, the format function allows format specifiers with or without the position fields to be mixed in the same format string. A format specifier without a position field always uses the next parameter after the last consumed parameter. In addition, the format function does not require that all function parameters be used in the format string. Example:
SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
Result: Testing three, two, oneThe %I and %L format specifiers are especially useful to safely construct dynamic SQL statements.
NLS_UPPER
Description
This function converts all letters in a string to uppercase letters.
Syntax
NLS_UPPER(char [, 'nlsparam' ])Parameters
Parameter | Description |
char | The string to be converted. The following data types are supported: |
Return value type
The return value is of the same data type as the value of the char parameter.
Example
select NLS_UPPER('aaa BBB CcC') FROM DUAL;
nls_upper
-------------
AAA BBB CCCNLSSORT
Description
This function returns a string that is sorted.
Syntax
NLSSORT(char [, 'nlsparam' ])Parameters
Parameter | Description |
char | The string to be sorted. The following data types are supported: |
nlsparam | Optional. The method in which the string is sorted. |
Return value type
The return value is of the same data type as the value of the char parameter.
Example
CREATE TABLE test (name VARCHAR2(15));
INSERT INTO test VALUES ('ccc');
INSERT INTO test VALUES ('bbb');
INSERT INTO test VALUES ('aaa');
SELECT * FROM test ORDER BY name;
name
-----------
Gaardiner
Gaasten
Gaberd
SELECT * FROM test ORDER BY NLSSORT(name, 'NLS_SORT = Danish');
name
-----------
Gaberd
Gaardiner
GaastenREGEXP_INSTR
Description
This function returns the position of the substring that matches a regular expression in a string.
Syntax
REGEXP_INSTR (source_char, pattern[, position[, occurrence[, return_opt[, match_param[, subexpr]]]]])Parameters
Parameter | Description |
source_char | The string to be searched. The following data types are supported: |
pattern | The regular expression. In most cases, the value is a text literal, and the following character types are supported: |
position | Optional. The positive integer that indicates the position from which the system starts to search for the substring that matches the regular expression. Default value: 1, which indicates that the search starts from the first character of the string specified by |
occurrence | Optional. The positive integer that indicates the occurrence of the substring that matches the regular expression in the string specified by |
return_opt | Optional. The option that specifies how the position of the occurrence is returned. Default value:
|
match_param | Optional. The default matching behavior of the function. The value is a character expression of the
|
subexpr |
|
Return value type
The return value is of the NUMBER data type.
Example
SELECT regexp_instr('abcdefghi', 'd.f') FROM DUAL;
regexp_instr
--------------
4INSTR
Description
This function returns the position of a substring in a string. A multi-byte character, such as a full-width character, is counted as one character.
Syntax
INSTR(char1,char2[,i[,j]])Parameters
Parameter | Description |
char1 | The string to be searched. The following data types are supported: |
char2 | The substring that you want to find. The following data types are supported: |
i | The position of the character from which the search starts. Default value: |
j | Optional. The integer that indicates the occurrence of the substring that you want to find. Default value: |
Return value type
The return value is of the NUMBER data type.
Example
SELECT INSTR('PolarDB-PG','P',1,2) "INSTR" FROM DUAL;
INSTR
-------
9TRIM
Description
This function removes the specified leading or trailing characters or both from a string.
The values of trim_character and trim_source can be of the VARCHAR2 data type or of the data types that can be implicitly converted to the VARCHAR2 data type. If the string that is returned by the function is of the VARCHAR2 data type, the maximum length of the string is the same as that of the string specified by trim_source.
Syntax
TRIM([{{ LEADING | TRAILING | BOTH }[ trim_character ]|trim_character }FROM] trim_source)Parameters
Parameter | Description |
LEADING | TRAILING | BOTH | The position of the characters to be removed.
|
trim_character | The character to be removed. You can specify only a single character. If |
trim_source | The string that you want to trim. If you specify only |
Return value type
If
trim_sourceis set to values of theCHARorVARCHAR2data type, the function returns values of theVARCHAR2data type.If
trim_sourceis set to values of theNCHARorNVARCHAR2data type, the function returns values of theNVARCHAR2data type.If
trim_sourceis set to values of theCLOBdata type, the function returns values of theCLOBdata type.If
trim_sourceortrim_characteris set toNULL, this function returnsNULL.
Example
SELECT trim(both 'x' from 'x!@#$x') FROM DUAL;
btrim
-------
!@#$TREAT
Description
This function changes the declared type of an expression. You must have the EXECUTE object permission on the new type to use this function.
The new type must be a supertype or subtype of the declared type of the expression. If the most specific type of the expression is the new type or a subtype of the new type, TREAT returns the expression. If the most specific type of the expression is not the new type or a subtype of the new type, TREAT returns NULL.
REF can be specified only if the declared type of the expression is a REF type.
If the declared type of the expression is a REF to the source type of the expression, the new type must be a subtype or supertype of the source type of the expression. If the most specific type of DEREF(expr) is the new type or a subtype of the new type, TREAT returns the expression. If the most specific type of DEREF(expr) is not the new type or a subtype of the new type, TREAT returns NULL.
This function does not directly support values of the CLOB data type. However, values of the CLOB data type can be passed in as parameters by using implicit type conversion.
Syntax
TREAT( expr AS [ REF ] [ schema. ]type)Parameters
Parameter | Description |
expr | The expression whose declared type you want to change. The following data types are supported: |
REF | Optional. The REF. You can specify this parameter only if the declared type of the expression is a REF type. |
schema | Optional. The schema of the new type. |
type | The new type. |
Return value type
The return value is of the same data type as the value of the expr parameter.
Example
SELECT name, TREAT(VALUE(p) AS employee_t).salary salary FROM persons p;
NAME SALARY
------------------------- ----------
Bob
Joe 100000
Tim 1000SOUNDEX
Description
This function is used to return a string that contains the phonetic representation of the specified string. You can use this function to compare words in English that are spelled differently but sound alike.
Syntax
SOUNDEX(char)Parameters
Parameter | Description |
char | The string for which you want to obtain the phonetic representation. The following data types are supported: |
Return value type
The return value is of the same data type as the value of the char parameter.
Example
create table test(c varchar(100));
insert into test values('Smith');
select c from test where SOUNDEX(c) = SOUNDEX('SMYTHE');
c
-------
SmithREGEEXP_SUBSTR
Description
This function returns the substring that matches a regular expression in a string, which extends the functionality of the SUBSTR function. This function works in a way similar to the REGEXP_INSTR function, but this function returns the substring instead of the position of the substring. This function is useful if you need to only obtain the content of a substring, but do not need to return its position in the source string.
Syntax
REGEXP_SUBSTR(source_char, pattern
[, position [, occurrence [, match_param [, subexpr] ] ] ]
)Parameters
Parameter | Description |
source_char | The string to be searched. The following data types are supported: |
pattern | The regular expression. In most cases, the value is a text literal, and the following data types are supported: |
position | Optional. The positive integer that indicates the position from which the system starts to search for the substring that matches the regular expression. Default value: 1, which indicates that the search starts from the first character of the string specified by |
occurrence | Optional. The positive integer that indicates the occurrence of the substring that matches the regular expression in the string specified by |
match_param | Optional. The default matching behavior of the function. The value is a character expression of the
|
subexpr | Optional. The subexpression that you want to use in all the subexpressions that are specified by |
Return value type
The return value is of the VARCHAR2 or CLOB data type in the same character set as source_char.
Example
SELECT REGEXP_SUBSTR('500 Oracle Parkway, Redwood Shores, CA',',[^,]+,') "REGEXPR_SUBSTR" FROM DUAL;
REGEXPR_SUBSTR
-------------------
, Redwood Shores,
SELECT REGEXP_SUBSTR('http://www.oracle.com/products','http://([[:alnum:]]+\.?){3,4}/?') "REGEXP_SUBSTR" FROM DUAL;
REGEXP_SUBSTR
------------------------
http://www.oracle.com/NLS_LOWER
Description
This function converts all letters in a string to lowercase letters.
Syntax
NLS_LOWER(char [, 'nlsparam' ])Parameters
Parameter | Description |
char | The string to be converted. The following data types are supported: |
Return value type
The return value is of the same data type as the value of the char parameter.
Example
select NLS_LOWER('aaa BBB CcC') FROM DUAL;
nls_lower
-------------
aaa bbb cccNLS_INTCAP
Description
This function returns a string with the first letter of each word in uppercase and the rest letters in lowercase. Words are separated with spaces or non-alphanumeric characters.
Syntax
NLS_INITCAP(c)Parameters
Parameter | Description |
c | The string that you want to convert. The following data types are supported: |
Return value type
The return value is of the same data type as the value of the c parameter.
Example
select INITCAP('aaa BBB CcC') FROM DUAL;
initcap
-------------
Aaa Bbb Ccc