String functions
Every string function Raft DB runs: syntax, one example each, and where it differs from MySQL or MariaDB.
A string function takes text (a number is converted to text first) and returns text or a number. NULL in gives NULL out unless the entry says otherwise. Positions start at 1. Text is UTF-8: CHAR_LENGTH counts characters, LENGTH counts bytes.
All text comparisons made by these functions (FIELD, FIND_IN_SET, INSTR, LOCATE, STRCMP) ignore case, as the default collation of both servers does (see Collation and case sensitivity). REPLACE is the exception: it is case-sensitive, as in MySQL.
Functions that exist in MySQL or MariaDB and not in Raft DB are listed on Functions that are not supported yet. The pattern functions REGEXP_* are on Regular expression functions, the hash functions on Encryption and hash functions.
Summary of functions#
| Function | What it does | Dialect |
|---|---|---|
ASCII | Returns the numeric value of the first byte of str, or 0 for an empty string. | MySQL and MariaDB |
BIN | Returns the binary digits of the whole number n as a string. | MySQL and MariaDB |
BIT_LENGTH | Returns the length of str in bits (eight times the number of bytes). | MySQL and MariaDB |
BTRIM | Removes leading and trailing white space from str. | MariaDB function; Raft DB also accepts it when it runs as MySQL |
CHAR | Returns the string made of the characters whose code points are the arguments. | MySQL and MariaDB |
CHAR_LENGTH | Returns the length of str in characters. | MySQL and MariaDB |
CHR | Returns the character whose code point is n. | MariaDB function; Raft DB also accepts it when it runs as MySQL |
CONCAT | Joins its arguments into one string. | MySQL and MariaDB |
CONCAT_WS | Joins the arguments with separator between them. | MySQL and MariaDB |
ELT | Returns the n-th string of the list, or NULL if n is less than 1 or greater than the number of strings. | MySQL and MariaDB |
EXPORT_SET | For every bit set in bits (lowest bit first) returns the string on, for every bit not set off, joined with separator (a comma by default); number_of_bits defaults to 64. | MySQL and MariaDB |
FIELD | Returns the position of str in the list str1, str2, ... (first is 1), or 0 if it is not there. | MySQL and MariaDB |
FIND_IN_SET | Returns the position of str in the comma-separated list strlist (first is 1), or 0. | MySQL and MariaDB |
FORMAT | Writes the number x with a thousands separator and d decimals, rounded. | MySQL and MariaDB |
FROM_BASE64 | Decodes a Base64 string. | MySQL and MariaDB |
HEX | For a string returns the hexadecimal digits of its bytes, for a number the hexadecimal digits of the number, in upper case. | MySQL and MariaDB |
INSERT | Returns str with the substring that starts at pos and is len characters long replaced by newstr. | MySQL and MariaDB |
INSTR | Returns the position of the first occurrence of substr in str (first character is 1), or 0. | MySQL and MariaDB |
LCASE | Synonym of LOWER. | MySQL and MariaDB |
LEFT | Returns the leftmost len characters of str. | MySQL and MariaDB |
LENGTH | Returns the length of str in bytes. | MySQL and MariaDB |
LENGTHB | Returns the length of str in bytes. | MariaDB function; Raft DB also accepts it when it runs as MySQL |
LOCATE | Returns the position of the first occurrence of substr in str, starting the search at pos (default 1), or 0. | MySQL and MariaDB |
LOWER | Returns str with every letter in lower case. | MySQL and MariaDB |
LPAD | Returns str padded on the left with padstr to the length len. | MySQL and MariaDB |
LTRIM | Removes leading white space from str. | MySQL and MariaDB |
MAKE_SET | Returns the strings whose bit is set in bits (bit 0 is str1), joined with commas. | MySQL and MariaDB |
MID | Synonym of SUBSTRING. | MySQL and MariaDB |
OCT | Returns the octal digits of the whole number n as a string. | MySQL and MariaDB |
ORD | Returns the code point of the first character of str, or 0 for an empty string. | MariaDB function; Raft DB also accepts it when it runs as MySQL |
QUOTE | Writes str as a string literal with quotes around it and a backslash before every ' and \. | MySQL and MariaDB |
REPEAT | Returns str repeated count times; an empty string if count is less than 1. | MySQL and MariaDB |
REPLACE | Returns str with every occurrence of from_str replaced by to_str. | MySQL and MariaDB |
REVERSE | Returns str with its characters in reverse order. | MySQL and MariaDB |
RIGHT | Returns the rightmost len characters of str. | MySQL and MariaDB |
RPAD | Returns str padded on the right with padstr to the length len; cut to len characters if it is longer. | MySQL and MariaDB |
RTRIM | Removes trailing white space from str. | MySQL and MariaDB |
SOUNDEX | Returns the Soundex code of str. | MySQL and MariaDB |
SPACE | Returns a string of n spaces. | MySQL and MariaDB |
STRCMP | Returns 0 if the strings are equal, -1 if the first is smaller, 1 if it is greater. | MySQL and MariaDB |
SUBSTR | Synonym of SUBSTRING. | MySQL and MariaDB |
SUBSTRING | Returns the part of str that starts at pos and is len characters long (to the end if len is left out). | MySQL and MariaDB |
SUBSTRING_INDEX | Returns the part of str before the count-th occurrence of delim; a negative count counts from the right and returns the part after it. | MySQL and MariaDB |
TO_BASE64 | Returns str encoded as Base64. | MySQL and MariaDB |
TRIM | Removes the characters remstr (a space by default) from the start, the end or both ends of str. | MySQL and MariaDB |
UCASE | Synonym of UPPER. | MySQL and MariaDB |
UNHEX | Reads each pair of hexadecimal digits of str as one byte. | MySQL and MariaDB |
UPPER | Returns str with every letter in upper case. | MySQL and MariaDB |
ASCII#
ASCII(str)Returns the numeric value of the first byte of str, or 0 for an empty string. NULL gives NULL.
Example:
SELECT ASCII('A');Result: 65
BIN#
BIN(n)Returns the binary digits of the whole number n as a string.
Example:
SELECT BIN(12);Result: 1100
BIT_LENGTH#
BIT_LENGTH(str)Returns the length of str in bits (eight times the number of bytes).
Example:
SELECT BIT_LENGTH('abc');Result: 24
BTRIM#
MariaDB function; Raft DB also accepts it when it runs as MySQL.
BTRIM(str)Removes leading and trailing white space from str. Same as TRIM(str).
Example:
SELECT BTRIM(' x ');Result: x
CHAR#
CHAR(n [, n] ...)Returns the string made of the characters whose code points are the arguments. A NULL argument is skipped. The USING charset form is not read.
Example:
SELECT CHAR(77, 121, 83, 81, '76');Result: MySQL
CHAR_LENGTH#
CHAR_LENGTH(str)
CHARACTER_LENGTH(str)Returns the length of str in characters. CHARACTER_LENGTH is a synonym.
Example:
SELECT CHAR_LENGTH('héllo');Result: 5
CHR#
MariaDB function; Raft DB also accepts it when it runs as MySQL.
CHR(n)Returns the character whose code point is n.
Example:
SELECT CHR(65);Result: A
CONCAT#
CONCAT(str1, str2, ...)Joins its arguments into one string. Numbers are written as text. The result is NULL if any argument is NULL.
Example:
SELECT CONCAT('a', 1, 'b');Result: a1b
CONCAT_WS#
CONCAT_WS(separator, str1, str2, ...)Joins the arguments with separator between them. NULL arguments after the separator are skipped. The result is NULL if the separator is NULL.
Example:
SELECT CONCAT_WS(',', 'a', NULL, 'c');Result: a,c
ELT#
ELT(n, str1, str2, ...)Returns the n-th string of the list, or NULL if n is less than 1 or greater than the number of strings.
Example:
SELECT ELT(2, 'a', 'b', 'c');Result: b
EXPORT_SET#
EXPORT_SET(bits, on, off [, separator [, number_of_bits]])For every bit set in bits (lowest bit first) returns the string on, for every bit not set off, joined with separator (a comma by default); number_of_bits defaults to 64.
Example:
SELECT EXPORT_SET(5, 'Y', 'N', ',', 4);Result: Y,N,Y,N
FIELD#
FIELD(str, str1, str2, ...)Returns the position of str in the list str1, str2, ... (first is 1), or 0 if it is not there. The comparison ignores case, like every text comparison of Raft DB.
Example:
SELECT FIELD('b', 'a', 'B');Result: 2
FIND_IN_SET#
FIND_IN_SET(str, strlist)Returns the position of str in the comma-separated list strlist (first is 1), or 0. The comparison ignores case.
Example:
SELECT FIND_IN_SET('b', 'a,b,c');Result: 2
FORMAT#
FORMAT(x, d [, locale])Writes the number x with a thousands separator and d decimals, rounded. The locale argument is accepted and ignored (the format is always the one of en_US).
Example:
SELECT FORMAT(1234567.891, 2);Result: 1,234,567.89
FROM_BASE64#
FROM_BASE64(str)Decodes a Base64 string. The result is NULL for a string that is not Base64.
Example:
SELECT FROM_BASE64('YWJj');Result: abc
HEX#
HEX(str)
HEX(n)For a string returns the hexadecimal digits of its bytes, for a number the hexadecimal digits of the number, in upper case.
Example:
SELECT HEX('abc'), HEX(255);Result: 616263, FF
INSERT#
INSERT(str, pos, len, newstr)Returns str with the substring that starts at pos and is len characters long replaced by newstr. If pos is outside the string, str is returned unchanged.
Example:
SELECT INSERT('Quadratic', 3, 4, 'What');Result: QuWhattic
INSTR#
INSTR(str, substr)Returns the position of the first occurrence of substr in str (first character is 1), or 0. The search ignores case.
Example:
SELECT INSTR('foobarbar', 'bar');Result: 4
LCASE#
LCASE(str)Synonym of LOWER.
Example:
SELECT LCASE('ABC');Result: abc
LEFT#
LEFT(str, len)Returns the leftmost len characters of str.
Example:
SELECT LEFT('foobarbar', 5);Result: fooba
LENGTH#
LENGTH(str)
OCTET_LENGTH(str)Returns the length of str in bytes. OCTET_LENGTH is a synonym. Text is UTF-8, so a character such as é counts as two bytes.
Example:
SELECT LENGTH('héllo');Result: 6
LENGTHB#
MariaDB function; Raft DB also accepts it when it runs as MySQL.
LENGTHB(str)Returns the length of str in bytes. Same as LENGTH.
Example:
SELECT LENGTHB('héllo');Result: 6
LOCATE#
LOCATE(substr, str [, pos])Returns the position of the first occurrence of substr in str, starting the search at pos (default 1), or 0. The search ignores case.
Example:
SELECT LOCATE('bar', 'foobarbar', 5);Result: 7
LOWER#
LOWER(str)Returns str with every letter in lower case.
Example:
SELECT LOWER('QUADRATICALLY');Result: quadratically
LPAD#
LPAD(str, len, padstr)Returns str padded on the left with padstr to the length len. If str is longer than len it is cut to len characters. NULL if padstr is empty.
Example:
SELECT LPAD('hi', 5, '?');Result: ???hi
LTRIM#
LTRIM(str)Removes leading white space from str.
Example:
SELECT LTRIM(' barbar');Result: barbar
MAKE_SET#
MAKE_SET(bits, str1, str2, ...)Returns the strings whose bit is set in bits (bit 0 is str1), joined with commas. NULL strings are skipped.
Example:
SELECT MAKE_SET(1 | 4, 'hello', 'nice', 'world');Result: hello,world
MID#
MID(str, pos [, len])Synonym of SUBSTRING.
Example:
SELECT MID('Quadratically', 5, 6);Result: ratica
OCT#
OCT(n)Returns the octal digits of the whole number n as a string.
Example:
SELECT OCT(12);Result: 14
ORD#
MariaDB function; Raft DB also accepts it when it runs as MySQL.
ORD(str)Returns the code point of the first character of str, or 0 for an empty string.
Example:
SELECT ORD('a'), ORD('é');Result: 97, 233
QUOTE#
QUOTE(str)Writes str as a string literal with quotes around it and a backslash before every ' and \. NULL gives the four letters NULL without quotes.
Example:
SELECT QUOTE('Don''t!');Result: 'Don\'t!'
REPEAT#
REPEAT(str, count)Returns str repeated count times; an empty string if count is less than 1.
Example:
SELECT REPEAT('ab', 3);Result: ababab
REPLACE#
REPLACE(str, from_str, to_str)Returns str with every occurrence of from_str replaced by to_str. The search is case-sensitive.
Example:
SELECT REPLACE('www.mysql.com', 'w', 'Ww');Result: WwWwWw.mysql.com
REVERSE#
REVERSE(str)Returns str with its characters in reverse order.
Example:
SELECT REVERSE('abc');Result: cba
RIGHT#
RIGHT(str, len)Returns the rightmost len characters of str.
Example:
SELECT RIGHT('foobarbar', 4);Result: rbar
RPAD#
RPAD(str, len, padstr)Returns str padded on the right with padstr to the length len; cut to len characters if it is longer.
Example:
SELECT RPAD('hi', 5, '?');Result: hi???
RTRIM#
RTRIM(str)Removes trailing white space from str.
Example:
SELECT RTRIM('barbar ');Result: barbar
SOUNDEX#
SOUNDEX(str)Returns the Soundex code of str.
Example:
SELECT SOUNDEX('Hello');Result: H400
SPACE#
SPACE(n)Returns a string of n spaces.
Example:
SELECT CONCAT('[', SPACE(3), ']');Result: [ ]
STRCMP#
STRCMP(str1, str2)Returns 0 if the strings are equal, -1 if the first is smaller, 1 if it is greater. The comparison ignores case.
Example:
SELECT STRCMP('text', 'text2');Result: -1
SUBSTR#
SUBSTR(str, pos [, len])Synonym of SUBSTRING.
Example:
SELECT SUBSTR('Sakila', -3);Result: ila
SUBSTRING#
SUBSTRING(str, pos [, len])
SUBSTRING(str FROM pos [FOR len])Returns the part of str that starts at pos and is len characters long (to the end if len is left out). A negative pos counts from the end; a pos of 0 gives an empty string.
Example:
SELECT SUBSTRING('Quadratically', 5), SUBSTRING('Sakila', -3, 2);Result: ratically, il
SUBSTRING_INDEX#
SUBSTRING_INDEX(str, delim, count)Returns the part of str before the count-th occurrence of delim; a negative count counts from the right and returns the part after it.
Example:
SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2), SUBSTRING_INDEX('www.mysql.com', '.', -1);Result: www.mysql, com
TO_BASE64#
TO_BASE64(str)Returns str encoded as Base64.
Example:
SELECT TO_BASE64('abc');Result: YWJj
TRIM#
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
TRIM(str)Removes the characters remstr (a space by default) from the start, the end or both ends of str. TRIM(str) removes white space. A remstr of several characters is read as a set of characters, each removed on its own, where MySQL removes the whole string.
Example:
SELECT TRIM(LEADING 'x' FROM 'xxabcxx');Result: abcxx
UCASE#
UCASE(str)Synonym of UPPER.
Example:
SELECT UCASE('abc');Result: ABC
UNHEX#
UNHEX(str)Reads each pair of hexadecimal digits of str as one byte. NULL if str is not hexadecimal or the bytes are not valid UTF-8 (a limit of Raft DB: text values are UTF-8).
Example:
SELECT UNHEX('4D7953514C');Result: MySQL
UPPER#
UPPER(str)Returns str with every letter in upper case.
Example:
SELECT UPPER('abc');Result: ABC