SQL reference (MySQL and MariaDB dialects)

Numeric functions

The mathematical functions of Raft DB: rounding, powers, logarithms, trigonometry and number bases.

The numeric functions take numbers; text that reads as a number is converted, other text counts as 0 (strict mode does not raise a warning for it in a SELECT). NULL in gives NULL out. Functions that cannot compute a value (SQRT(-1), LN(0), ACOS(2)) answer NULL, as MySQL does outside strict division checks.

Exact arithmetic (+ - * / % DIV on whole numbers and DECIMAL) is done in 128 bits, so a result is exact up to that size; the functions below that give a double (POW, EXP, the logarithms and the trigonometric functions) are floating point. The operators are on Arithmetic and logical operators.

Summary of functions#

FunctionWhat it doesDialect
ABSReturns the absolute value of x.MySQL and MariaDB
ACOSReturns the arc cosine of x in radians.MySQL and MariaDB
ASINReturns the arc sine of x in radians.MySQL and MariaDB
ATANWith one argument returns the arc tangent of x; with two arguments the arc tangent of y / x, using the signs of both to find the quadrant.MySQL and MariaDB
ATAN2The angle, in radians, whose tangent is y / x; the sign of each argument picks the quadrant the angle lies in.MySQL and MariaDB
BIT_COUNTReturns the number of bits set in the whole number n.MySQL and MariaDB
CEILReturns the smallest whole number not less than x.MySQL and MariaDB
CONVConverts the number n, written as a string in base from_base, to a string in base to_base (2 to 36; a negative to_base writes a signed result).MySQL and MariaDB
COSReturns the cosine of x (in radians).MySQL and MariaDB
COTReturns the cotangent of x.MySQL and MariaDB
CRC32Returns the 32-bit cyclic redundancy check of a string as an unsigned number.MySQL and MariaDB
DEGREESConverts x from radians to degrees.MySQL and MariaDB
EXPReturns e raised to the power x.MySQL and MariaDB
FLOORReturns the largest whole number not greater than x.MySQL and MariaDB
GREATESTReturns the largest argument.MySQL and MariaDB
INTERVALReturns 0 if n is less than n1, 1 if it is less than n2, and so on: the number of the arguments n1, n2, ... that are less than or equal to n.MySQL and MariaDB
LEASTReturns the smallest argument.MySQL and MariaDB
LNReturns the natural logarithm of x; NULL if x is not greater than 0.MySQL and MariaDB
LOGWith one argument returns the natural logarithm of x; with two arguments the logarithm of x in base b.MySQL and MariaDB
LOG10Returns the base-10 logarithm of x.MySQL and MariaDB
LOG2Returns the base-2 logarithm of x.MySQL and MariaDB
MODReturns the remainder of n divided by m.MySQL and MariaDB
PIReturns the value of pi as a double (3.141592653589793).MySQL and MariaDB
POWReturns x raised to the power y (a double).MySQL and MariaDB
RADIANSConverts x from degrees to radians.MySQL and MariaDB
RANDReturns a random double from 0 up to but not including 1.MySQL and MariaDB
ROUNDRounds x to d decimals (0 by default; a negative d rounds to the left of the decimal point).MySQL and MariaDB
SIGNReturns -1, 0 or 1 as x is negative, zero or positive.MySQL and MariaDB
SINReturns the sine of x (in radians).MySQL and MariaDB
SQRTReturns the square root of x; NULL if x is negative.MySQL and MariaDB
TANReturns the tangent of x (in radians).MySQL and MariaDB
TRUNCATEReturns x with the decimals after the d-th cut off (no rounding).MySQL and MariaDB

ABS#

SQL
ABS(x)

Returns the absolute value of x.

Example:

SQL
SELECT ABS(-32);

Result: 32

ACOS#

SQL
ACOS(x)

Returns the arc cosine of x in radians. NULL if x is outside -1 to 1.

Example:

SQL
SELECT ACOS(1), ACOS(2);

Result: 0, NULL

ASIN#

SQL
ASIN(x)

Returns the arc sine of x in radians. NULL if x is outside -1 to 1.

Example:

SQL
SELECT ASIN(1);

Result: 1.5707963267948966

ATAN#

SQL
ATAN(x)
ATAN(y, x)

With one argument returns the arc tangent of x; with two arguments the arc tangent of y / x, using the signs of both to find the quadrant.

Example:

SQL
SELECT ATAN(1);

Result: 0.7853981633974483

ATAN2#

SQL
ATAN2(y, x)

The angle, in radians, whose tangent is y / x; the sign of each argument picks the quadrant the angle lies in.

Example:

SQL
SELECT ATAN2(1, 1);

Result: 0.7853981633974483

BIT_COUNT#

SQL
BIT_COUNT(n)

Returns the number of bits set in the whole number n.

Example:

SQL
SELECT BIT_COUNT(29);

Result: 4

CEIL#

SQL
CEIL(x)
CEILING(x)

Returns the smallest whole number not less than x. CEILING is a synonym.

Example:

SQL
SELECT CEIL(1.23), CEIL(-1.23);

Result: 2, -1

CONV#

SQL
CONV(n, from_base, to_base)

Converts the number n, written as a string in base from_base, to a string in base to_base (2 to 36; a negative to_base writes a signed result).

Example:

SQL
SELECT CONV('a', 16, 2);

Result: 1010

COS#

SQL
COS(x)

Returns the cosine of x (in radians).

Example:

SQL
SELECT COS(0);

Result: 1

COT#

SQL
COT(x)

Returns the cotangent of x.

Example:

SQL
SELECT COT(1);

Result: 0.6420926159343306

CRC32#

SQL
CRC32(expr)

Returns the 32-bit cyclic redundancy check of a string as an unsigned number.

Example:

SQL
SELECT CRC32('MySQL');

Result: 3259397556

DEGREES#

SQL
DEGREES(x)

Converts x from radians to degrees.

Example:

SQL
SELECT DEGREES(PI());

Result: 180

EXP#

SQL
EXP(x)

Returns e raised to the power x.

Example:

SQL
SELECT EXP(0);

Result: 1

FLOOR#

SQL
FLOOR(x)

Returns the largest whole number not greater than x.

Example:

SQL
SELECT FLOOR(-1.5);

Result: -2

GREATEST#

SQL
GREATEST(value1, value2, ...)

Returns the largest argument. The result is NULL if any argument is NULL.

Example:

SQL
SELECT GREATEST(1, 5, 3);

Result: 5

INTERVAL#

SQL
INTERVAL(n, n1, n2, ...)

Returns 0 if n is less than n1, 1 if it is less than n2, and so on: the number of the arguments n1, n2, ... that are less than or equal to n. The list must be in increasing order. Returns -1 for NULL. (This is the function; the INTERVAL expr unit of date arithmetic is on Date and time functions.)

Example:

SQL
SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);

Result: 3

LEAST#

SQL
LEAST(value1, value2, ...)

Returns the smallest argument. The result is NULL if any argument is NULL.

Example:

SQL
SELECT LEAST(2, 0);

Result: 0

LN#

SQL
LN(x)

Returns the natural logarithm of x; NULL if x is not greater than 0.

Example:

SQL
SELECT LN(1);

Result: 0

LOG#

SQL
LOG(x)
LOG(b, x)

With one argument returns the natural logarithm of x; with two arguments the logarithm of x in base b. NULL for x not greater than 0 and for b not greater than 0 or equal to 1.

Example:

SQL
SELECT LOG(10);

Result: 2.302585092994046

LOG10#

SQL
LOG10(x)

Returns the base-10 logarithm of x.

Example:

SQL
SELECT LOG10(100);

Result: 2

LOG2#

SQL
LOG2(x)

Returns the base-2 logarithm of x.

Example:

SQL
SELECT LOG2(8);

Result: 3

MOD#

SQL
MOD(n, m)
n % m
n MOD m

Returns the remainder of n divided by m. The result is NULL if m is 0.

Example:

SQL
SELECT MOD(10, 3), 10 % 3;

Result: 1, 1

PI#

SQL
PI()

Returns the value of pi as a double (3.141592653589793).

Example:

SQL
SELECT PI();

Result: about 3.14159

POW#

SQL
POW(x, y)
POWER(x, y)

Returns x raised to the power y (a double). POWER is a synonym.

Example:

SQL
SELECT POW(2, 10);

Result: 1024

RADIANS#

SQL
RADIANS(x)

Converts x from degrees to radians.

Example:

SQL
SELECT RADIANS(180);

Result: 3.141592653589793

RAND#

SQL
RAND()

Returns a random double from 0 up to but not including 1. A seed argument is accepted and ignored: the values are not repeatable.

Example:

SQL
SELECT RAND() < 1;

Result: 1

ROUND#

SQL
ROUND(x [, d])

Rounds x to d decimals (0 by default; a negative d rounds to the left of the decimal point). An exact number (a DECIMAL or a literal such as 2.5) rounds half away from zero; a FLOAT or DOUBLE rounds half to even, as the C library does.

Example:

SQL
SELECT ROUND(2.5), ROUND(2.5e0), ROUND(1.298, 1);

Result: 3, 2, 1.3

SIGN#

SQL
SIGN(x)

Returns -1, 0 or 1 as x is negative, zero or positive.

Example:

SQL
SELECT SIGN(-5);

Result: -1

SIN#

SQL
SIN(x)

Returns the sine of x (in radians).

Example:

SQL
SELECT SIN(0);

Result: 0

SQRT#

SQL
SQRT(x)

Returns the square root of x; NULL if x is negative.

Example:

SQL
SELECT SQRT(16);

Result: 4

TAN#

SQL
TAN(x)

Returns the tangent of x (in radians).

Example:

SQL
SELECT TAN(0);

Result: 0

TRUNCATE#

SQL
TRUNCATE(x, d)

Returns x with the decimals after the d-th cut off (no rounding).

Example:

SQL
SELECT TRUNCATE(1.999, 1);

Result: 1.9

See also#