CHAR and VARCHAR data types). See Character types.
Except where noted, these functions and operators are declared to accept and return type VARCHAR.
Notes:
- Text comparison is case-sensitive (e.g., ‘Regatta’
<>’REGATTA’<>‘regatta’ and ‘A’ < ‘B’ < ‘a’ < ‘b’). - Values of type
CHARare converted toVARCHARbefore the function or operator is applied, resulting in stripping any trailing spaces in the character value.
| Function | Description and Comments | Example |
|---|---|---|
string || string | Concatenate of two strings. See also CONCAT(). See note 1. | 'This in an ' || 'example' => 'This in an example' |
CONCAT(string, string [, ...]) | Concatenate two or more strings. See note 2. | CONCAT('Hello', ' it', ' is me')=> 'Hello it is me' |
LOWER(string) | Convert a string to all lowercase | LOWER('Hello')=> 'hello' |
UPPER(string) | Convert a string to all uppercase | UPPER('Hello')=> 'HELLO' |
POSITION(sub_string IN string) | Return the first starting index of the specified substring within the string, or zero if it’s not present. The return type is INT | POSITION('o' IN 'Hello')=> 5 |
SUBSTRING(string, start [, count] )SUBSTRING([FROM start] [FOR count]) | Extracts a substring from a given string. See note 3 | SUBSTRING(‘hello’, 2, 3) => ‘ell’SUBSTRING(‘hello’, 2) => ‘ello’SUBSTRING(‘hello’ FROM 2 FOR 3) => ‘ell’SUBSTRING(‘hello’ FROM 2) => ‘ello’SUBSTRING ‘hello’ FOR 3) => ‘hel’ |
TRIM( [LEADING | TRAILING | BOTH] { [trim_chars FROM] | [FROM] } string)TRIM([LEADING | TRAILING | BOTH] [FROM] string [, trim_chars]) | Remove the longest string containing only characters in trim_chars (space by default), from either the front or the back of the input string, or both (the default). See note 4. | TRIM(LEADING ‘xyz’ FROM ‘zxxzhello’)=> ‘hello’ TRIM(BOTH ‘xyz’ FROM ‘yxthis is mezy’)=> ‘this is me’ TRIM(‘ hello ’)=> ‘hello’ TRIM(‘__hello__’, ‘_’)=> ‘hello’ |
BTRIM(STRING [, trim_chars]) | Alias of TRIM() with BOTH | BTRIM('xthis_is_mez', 'xz')=> 'this_is_me'BTRIM(' hello ') = TRIM(' hello ')=> true |
LTRIM(STRING [, trim_chars]) | Alias of TRIM() with LEADING | LTRIM('xthis_is_mez', 'xz')=> 'this_is_mez' |
RTRIM(STRING [, trim_chars]) | Alias of TRIM() with TRAILING | RTRIM('xthis_is_mez', 'xz')=> 'xthis_is_me' |
LEFT(string, count) | Return the first count characters of a string. If count is negative, return all but last count characters. | LEFT('This is me', 8)=> 'This is 'LEFT('This is me', -8)=> 'Th' |
RIGHT(string, count) | Return the last count characters of a string. If count is negative, return all but the first count characters. | RIGHT('This is me', 8)=> 'is is me'RIGHT('This is me', -8)=> 'me' |
LENGTH(string) | Return the number of characters in a string. The return type is INT. | LENGTH('This is me')=> 10 |
LPAD(string, length [, fill_string]) | Extend a string to a given length by prepending the fill_string (a space by default). See note 5. | LPAD('Hello', 10, '-')=> '-----Hello'LPAD('Hello', 10)=> ' Hello' |
RPAD(string, length [, fill_string]) | Extend a string to a given length by appending the fill_string (a space by default). See note 5. | RPAD('Hello', 10, '12')=> 'Hello12121' |
REPEAT(string, count) | Repeats a string the specified number of times | REPEAT('Hello', 3)=> 'HelloHelloHello' |
REPLACE(string, from, to) | Replace all occurrences of substring from with substring to | REPLACE('Hello, this is me', 'me', 'you')=> 'Hello, this is you' |
REVERSE(string) | Reverse the order of the characters in a string | REVERSE('Hello')=> 'olleH' |
string [NOT] LIKE pattern [ESCAPE escape-char] | Match a string expression by a pattern. The match is case sensitive. Returns BOOLEAN. See note 6. | 'Regatta' LIKE 'Rega%'=> true 'Regatta' LIKE '_e_a%a'=> true ‘20% discount’ LIKE ’20!%’ ESCAPE ‘!’=> false ‘20% discount’ LIKE ’20!%%’ ESCAPE ‘!’=> true |
- The string concatenation operator (
||) accepts non-string input, so long as at least one input is of string type. Using the operator withNULLas one of the operator inputs, returns NULL. For example:SELECT 'Hello' || NULL || ' this is me';returnsNULL CONCAT():- All values are implicitly converted to strings
NULLarguments are ignored. For example:SELECT CONCAT('Hello', NULL, ' this is me');Return: ‘Hello this is me’
SUBSTRING(string, start [, count])receives start position and optional count. If count is omitted, the extracted substring is until the end of the input string.SUBSTRING(string [FROM start] [FOR count])– both FROM and FOR are optional but at least one of them should be included. If FROM is omitted, start from the beginning of the input string. IfFOR countis omitted, extract until the end of the input string.
If the FROM argument is less than 1, the returned substring begins from position 1 but the difference is subtracted from the count. For example,SELECT SUBSTRING(‘hello’, FROM -1 FOR 3)returns‘h’.TRIM()– In the first form,‘FROM’is mandatory iftrim_charsis included and optional otherwise. In the second form it’s optional.LPAD()(RPAD()): If the input string is longer than the requestedlengththen it is truncated on the right (left).LIKEandNOT LIKE:- String match is case sensitive
- Escape character should be a single character.
- Pattern special characters:
- An underscore (_) stands for any single character
- A percent sign (%) stands for any sequence of zero or more characters
- If the pattern does not contain percent signs (%) or underscores (_), it becomes a test for exact match of two strings.