SQL reference (MySQL and MariaDB dialects)

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#

FunctionWhat it doesDialect
ASCIIReturns the numeric value of the first byte of str, or 0 for an empty string.MySQL and MariaDB
BINReturns the binary digits of the whole number n as a string.MySQL and MariaDB
BIT_LENGTHReturns the length of str in bits (eight times the number of bytes).MySQL and MariaDB
BTRIMRemoves leading and trailing white space from str.MariaDB function; Raft DB also accepts it when it runs as MySQL
CHARReturns the string made of the characters whose code points are the arguments.MySQL and MariaDB
CHAR_LENGTHReturns the length of str in characters.MySQL and MariaDB
CHRReturns the character whose code point is n.MariaDB function; Raft DB also accepts it when it runs as MySQL
CONCATJoins its arguments into one string.MySQL and MariaDB
CONCAT_WSJoins the arguments with separator between them.MySQL and MariaDB
ELTReturns 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_SETFor 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
FIELDReturns the position of str in the list str1, str2, ... (first is 1), or 0 if it is not there.MySQL and MariaDB
FIND_IN_SETReturns the position of str in the comma-separated list strlist (first is 1), or 0.MySQL and MariaDB
FORMATWrites the number x with a thousands separator and d decimals, rounded.MySQL and MariaDB
FROM_BASE64Decodes a Base64 string.MySQL and MariaDB
HEXFor a string returns the hexadecimal digits of its bytes, for a number the hexadecimal digits of the number, in upper case.MySQL and MariaDB
INSERTReturns str with the substring that starts at pos and is len characters long replaced by newstr.MySQL and MariaDB
INSTRReturns the position of the first occurrence of substr in str (first character is 1), or 0.MySQL and MariaDB
LCASESynonym of LOWER.MySQL and MariaDB
LEFTReturns the leftmost len characters of str.MySQL and MariaDB
LENGTHReturns the length of str in bytes.MySQL and MariaDB
LENGTHBReturns the length of str in bytes.MariaDB function; Raft DB also accepts it when it runs as MySQL
LOCATEReturns the position of the first occurrence of substr in str, starting the search at pos (default 1), or 0.MySQL and MariaDB
LOWERReturns str with every letter in lower case.MySQL and MariaDB
LPADReturns str padded on the left with padstr to the length len.MySQL and MariaDB
LTRIMRemoves leading white space from str.MySQL and MariaDB
MAKE_SETReturns the strings whose bit is set in bits (bit 0 is str1), joined with commas.MySQL and MariaDB
MIDSynonym of SUBSTRING.MySQL and MariaDB
OCTReturns the octal digits of the whole number n as a string.MySQL and MariaDB
ORDReturns 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
QUOTEWrites str as a string literal with quotes around it and a backslash before every ' and \.MySQL and MariaDB
REPEATReturns str repeated count times; an empty string if count is less than 1.MySQL and MariaDB
REPLACEReturns str with every occurrence of from_str replaced by to_str.MySQL and MariaDB
REVERSEReturns str with its characters in reverse order.MySQL and MariaDB
RIGHTReturns the rightmost len characters of str.MySQL and MariaDB
RPADReturns str padded on the right with padstr to the length len; cut to len characters if it is longer.MySQL and MariaDB
RTRIMRemoves trailing white space from str.MySQL and MariaDB
SOUNDEXReturns the Soundex code of str.MySQL and MariaDB
SPACEReturns a string of n spaces.MySQL and MariaDB
STRCMPReturns 0 if the strings are equal, -1 if the first is smaller, 1 if it is greater.MySQL and MariaDB
SUBSTRSynonym of SUBSTRING.MySQL and MariaDB
SUBSTRINGReturns 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_INDEXReturns 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_BASE64Returns str encoded as Base64.MySQL and MariaDB
TRIMRemoves the characters remstr (a space by default) from the start, the end or both ends of str.MySQL and MariaDB
UCASESynonym of UPPER.MySQL and MariaDB
UNHEXReads each pair of hexadecimal digits of str as one byte.MySQL and MariaDB
UPPERReturns str with every letter in upper case.MySQL and MariaDB

ASCII#

SQL
ASCII(str)

Returns the numeric value of the first byte of str, or 0 for an empty string. NULL gives NULL.

Example:

SQL
SELECT ASCII('A');

Result: 65

BIN#

SQL
BIN(n)

Returns the binary digits of the whole number n as a string.

Example:

SQL
SELECT BIN(12);

Result: 1100

BIT_LENGTH#

SQL
BIT_LENGTH(str)

Returns the length of str in bits (eight times the number of bytes).

Example:

SQL
SELECT BIT_LENGTH('abc');

Result: 24

BTRIM#

MariaDB function; Raft DB also accepts it when it runs as MySQL.

SQL
BTRIM(str)

Removes leading and trailing white space from str. Same as TRIM(str).

Example:

SQL
SELECT BTRIM('  x  ');

Result: x

CHAR#

SQL
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:

SQL
SELECT CHAR(77, 121, 83, 81, '76');

Result: MySQL

CHAR_LENGTH#

SQL
CHAR_LENGTH(str)
CHARACTER_LENGTH(str)

Returns the length of str in characters. CHARACTER_LENGTH is a synonym.

Example:

SQL
SELECT CHAR_LENGTH('héllo');

Result: 5

CHR#

MariaDB function; Raft DB also accepts it when it runs as MySQL.

SQL
CHR(n)

Returns the character whose code point is n.

Example:

SQL
SELECT CHR(65);

Result: A

CONCAT#

SQL
CONCAT(str1, str2, ...)

Joins its arguments into one string. Numbers are written as text. The result is NULL if any argument is NULL.

Example:

SQL
SELECT CONCAT('a', 1, 'b');

Result: a1b

CONCAT_WS#

SQL
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:

SQL
SELECT CONCAT_WS(',', 'a', NULL, 'c');

Result: a,c

ELT#

SQL
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:

SQL
SELECT ELT(2, 'a', 'b', 'c');

Result: b

EXPORT_SET#

SQL
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:

SQL
SELECT EXPORT_SET(5, 'Y', 'N', ',', 4);

Result: Y,N,Y,N

FIELD#

SQL
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:

SQL
SELECT FIELD('b', 'a', 'B');

Result: 2

FIND_IN_SET#

SQL
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:

SQL
SELECT FIND_IN_SET('b', 'a,b,c');

Result: 2

FORMAT#

SQL
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:

SQL
SELECT FORMAT(1234567.891, 2);

Result: 1,234,567.89

FROM_BASE64#

SQL
FROM_BASE64(str)

Decodes a Base64 string. The result is NULL for a string that is not Base64.

Example:

SQL
SELECT FROM_BASE64('YWJj');

Result: abc

HEX#

SQL
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:

SQL
SELECT HEX('abc'), HEX(255);

Result: 616263, FF

INSERT#

SQL
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:

SQL
SELECT INSERT('Quadratic', 3, 4, 'What');

Result: QuWhattic

INSTR#

SQL
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:

SQL
SELECT INSTR('foobarbar', 'bar');

Result: 4

LCASE#

SQL
LCASE(str)

Synonym of LOWER.

Example:

SQL
SELECT LCASE('ABC');

Result: abc

LEFT#

SQL
LEFT(str, len)

Returns the leftmost len characters of str.

Example:

SQL
SELECT LEFT('foobarbar', 5);

Result: fooba

LENGTH#

SQL
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:

SQL
SELECT LENGTH('héllo');

Result: 6

LENGTHB#

MariaDB function; Raft DB also accepts it when it runs as MySQL.

SQL
LENGTHB(str)

Returns the length of str in bytes. Same as LENGTH.

Example:

SQL
SELECT LENGTHB('héllo');

Result: 6

LOCATE#

SQL
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:

SQL
SELECT LOCATE('bar', 'foobarbar', 5);

Result: 7

LOWER#

SQL
LOWER(str)

Returns str with every letter in lower case.

Example:

SQL
SELECT LOWER('QUADRATICALLY');

Result: quadratically

LPAD#

SQL
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:

SQL
SELECT LPAD('hi', 5, '?');

Result: ???hi

LTRIM#

SQL
LTRIM(str)

Removes leading white space from str.

Example:

SQL
SELECT LTRIM('  barbar');

Result: barbar

MAKE_SET#

SQL
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:

SQL
SELECT MAKE_SET(1 | 4, 'hello', 'nice', 'world');

Result: hello,world

MID#

SQL
MID(str, pos [, len])

Synonym of SUBSTRING.

Example:

SQL
SELECT MID('Quadratically', 5, 6);

Result: ratica

OCT#

SQL
OCT(n)

Returns the octal digits of the whole number n as a string.

Example:

SQL
SELECT OCT(12);

Result: 14

ORD#

MariaDB function; Raft DB also accepts it when it runs as MySQL.

SQL
ORD(str)

Returns the code point of the first character of str, or 0 for an empty string.

Example:

SQL
SELECT ORD('a'), ORD('é');

Result: 97, 233

QUOTE#

SQL
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:

SQL
SELECT QUOTE('Don''t!');

Result: 'Don\'t!'

REPEAT#

SQL
REPEAT(str, count)

Returns str repeated count times; an empty string if count is less than 1.

Example:

SQL
SELECT REPEAT('ab', 3);

Result: ababab

REPLACE#

SQL
REPLACE(str, from_str, to_str)

Returns str with every occurrence of from_str replaced by to_str. The search is case-sensitive.

Example:

SQL
SELECT REPLACE('www.mysql.com', 'w', 'Ww');

Result: WwWwWw.mysql.com

REVERSE#

SQL
REVERSE(str)

Returns str with its characters in reverse order.

Example:

SQL
SELECT REVERSE('abc');

Result: cba

SQL
RIGHT(str, len)

Returns the rightmost len characters of str.

Example:

SQL
SELECT RIGHT('foobarbar', 4);

Result: rbar

RPAD#

SQL
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:

SQL
SELECT RPAD('hi', 5, '?');

Result: hi???

RTRIM#

SQL
RTRIM(str)

Removes trailing white space from str.

Example:

SQL
SELECT RTRIM('barbar   ');

Result: barbar

SOUNDEX#

SQL
SOUNDEX(str)

Returns the Soundex code of str.

Example:

SQL
SELECT SOUNDEX('Hello');

Result: H400

SPACE#

SQL
SPACE(n)

Returns a string of n spaces.

Example:

SQL
SELECT CONCAT('[', SPACE(3), ']');

Result: [ ]

STRCMP#

SQL
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:

SQL
SELECT STRCMP('text', 'text2');

Result: -1

SUBSTR#

SQL
SUBSTR(str, pos [, len])

Synonym of SUBSTRING.

Example:

SQL
SELECT SUBSTR('Sakila', -3);

Result: ila

SUBSTRING#

SQL
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:

SQL
SELECT SUBSTRING('Quadratically', 5), SUBSTRING('Sakila', -3, 2);

Result: ratically, il

SUBSTRING_INDEX#

SQL
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:

SQL
SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2), SUBSTRING_INDEX('www.mysql.com', '.', -1);

Result: www.mysql, com

TO_BASE64#

SQL
TO_BASE64(str)

Returns str encoded as Base64.

Example:

SQL
SELECT TO_BASE64('abc');

Result: YWJj

TRIM#

SQL
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:

SQL
SELECT TRIM(LEADING 'x' FROM 'xxabcxx');

Result: abcxx

UCASE#

SQL
UCASE(str)

Synonym of UPPER.

Example:

SQL
SELECT UCASE('abc');

Result: ABC

UNHEX#

SQL
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:

SQL
SELECT UNHEX('4D7953514C');

Result: MySQL

UPPER#

SQL
UPPER(str)

Returns str with every letter in upper case.

Example:

SQL
SELECT UPPER('abc');

Result: ABC

See also#