Skip to content

Numeric and mathematical functions

View as Markdown
ABS (number_expr)

The ABS function returns the absolute value of a given number. The data type of the return value is the same as that of the argument. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number_expr: An operator which returns a numeric value

Return type: same as that of the argument

--it returns the absolute value of the argument
SELECT ABS(12.3), ABS(-12.3), ABS(-12.3000), ABS(0.0);
ABS(12.3) ABS(-12.3) ABS(-12.3000) ABS(0.0)
12.3 12.3 12.3000 0.0
ACOS ( x )

The ACOS function returns an arc cosine value of the argument. That is, it returns a value whose cosine is x in radian. The return value is a DOUBLE type. x must be a value between -1 and 1, inclusive. Otherwise, NULL is returned. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT ACOS(1), ACOS(0), ACOS(-1);
ACOS(1) ACOS(0) ACOS(-1)
0 1.570796326794896 3.141592653589793
ASIN ( x )

The ASIN function returns an arc sine value of the argument. That is, it returns a value whose sine is x in radian. The return value is a DOUBLE type. x must be a value between -1 and 1, inclusive. Otherwise, NULL is returned. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT ASIN(1), ASIN(0), ASIN(-1);
ASIN(1) ASIN(0) ASIN(-1)
1.570796326794896 0 -1.570796326794896
ATAN ( [y,] x )

The ATAN function returns a value whose tangent is x in radian. The argument y can be omitted. If y is specified, the function calculates the arc tangent value of y/x. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x,y: An expression that returns a numeric value

Return type: DOUBLE

SELECT ATAN(1), ATAN(-1), ATAN(1,-1);
ATAN(1) ATAN(-1) ATAN(1,-1)
0.7853981633974483 -0.7853981633974483 2.356194490192345
ATAN2 ( y, x )

The ATAN2 function returns the arc tangent value of y/x in radian. This function is working like the ATAN. Arguments x and y must be specified. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x,y: An expression that returns a numeric value

Return type: DOUBLE

SELECT ATAN2(1,1), ATAN2(-1,-1), ATAN2(Pi(),0);
ATAN2(1,1) ATAN2(-1,-1) ATAN2(Pi(),0)
0.7853981633974483 -2.356194490192345 1.570796326794896
CEIL( number_operand )

The CEIL function returns the smallest integer that is not less than its argument. The return value is determined based on the valid number of digits that are specified as the number_operand argument. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number_operand: An expression that returns a numeric value

Return type: INT

SELECT CEIL(34567.34567), CEIL(-34567.34567), CEIL(34567.1), CEIL(-34567.1);
CEIL(34567.34567) CEIL(-34567.34567) CEIL(34567.1) CEIL(-34567.1)
34568.00000 -34567.00000 34568.0 -34567.0

The result keeps the scale of the argument.

CONV (number,from_base,to_base)

The CONV function converts numbers between different number bases. This function returns a string representation of a converted number. The minimum value is 2 and the maximum value is 36. If to_base (representing the base to be returned) is negative, number is regarded as a signed number. Otherwise, it regarded as a unsigned number. When you input the string which cannot be transformed into the number to from_base or to_base, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number: An input number
  • from_base: The base of an input number
  • to_base: The base of an returned value

Return type: STRING

SELECT CONV('f',16,2);
CONV('f',16,2)
1111
SELECT CONV('6H',20,8);
CONV('6H',20,8)
211
SELECT CONV(-30,10,-20);
CONV(-30,10,-20)
-1A
COS ( x )

The COS function returns a cosine value of the argument. The argument x must be a radian value. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT COS(pi()/6), COS(pi()/3), COS(pi());
COS(pi()/6) COS(pi()/3) COS(pi())
0.8660254037844387 0.5000000000000001 -1
COT ( x )

The COT function returns the cotangent value of the argument x. That is, it returns a value whose tangent is x in radian. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT COT(1), COT(-1), COT(0);
COT(1) COT(-1) COT(0)
0.6420926159343306 -0.6420926159343306 NULL
CRC32 ( string )

The CRC32 function returns a cyclic redundancy check value as 32-bit integer. When NULL is given as input, it returns NULL.

Parameters

  • string: An expression that returns a string value

Return type: INTEGER

SELECT CRC32('coradb');
CRC32('coradb')
248404764
DEGREES ( x )

The DEGREES function returns the argument x specified in radian converted to a degree value. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT DEGREES(pi()/6), DEGREES(pi()/3), DEGREES (pi());
DEGREES(pi()/6) DEGREES(pi()/3) DEGREES (pi())
29.99999999999999 59.99999999999999 180

DRANDOM ( [seed] )

DRAND ( [seed] )

The function DRANDOM or DRAND returns a random double-precision floating point value in the range of between 0.0 and 1.0. A seed argument that is INTEGER type can be specified. It rounds up real numbers and an error is returned when it exceeds the range of INTEGER.

When seed value is not given, the DRAND function performs the operation only once to produce only one random number regardless of the number of rows where the operation is output, but the DRANDOM function performs the operation every time the statement is repeated to produce a different random value for each row. Therefore, to output rows in a random order, you must use the DRANDOM function in the ORDER BY clause. To obtain a random integer value, use the RANDOM.

Parameters

  • seed: seed value

Return type: DOUBLE

SELECT DRAND(), DRAND(1), DRAND(1.4);
0.2849646518006921 0.041630344771878214 0.041630344771878214

The first value changes on every run. With a seed, the result is repeatable: DRAND(1) always returns the same number, and 1.4 is rounded to the seed 1.

DRAND() is evaluated once per statement, while DRANDOM() is evaluated for every row. Use DRANDOM() to sort rows in random order, for example to pick three random products:

SELECT id, name FROM product ORDER BY DRANDOM() LIMIT 3;
EXP( x )

The EXP function returns e x (the base of natural logarithm) raised to a power. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An operator which returns a numeric value

Return type: DOUBLE

SELECT EXP(1), EXP(0);
EXP(1) EXP(0)
2.718281828459045 1
SELECT EXP(-1), EXP(2.00);
EXP(-1) EXP(2.00)
0.3678794411714423 7.38905609893065
FLOOR( number_operand )

The FLOOR function returns the largest integer that is not greater than its argument. The data type of the return value is the same as that of the argument. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number_operand: An operator which returns a numeric value

Return type: same as that of the argument

--it returns the largest integer less than or equal to the arguments
SELECT FLOOR(34567.34567), FLOOR(-34567.34567);
FLOOR(34567.34567) FLOOR(-34567.34567)
34567.00000 -34568.00000
SELECT FLOOR(34567), FLOOR(-34567);
FLOOR(34567) FLOOR(-34567)
34567 -34567
HEX(n)

The HEX function returns a hexadecimal string about the string which is specified as an argument; it returns a hexadecimal string of the number if a number is specified as an argument. If a number is specified as an argument, it returns a value like CONV(num, 10, 16).

Parameters

  • n: A string or a number

Return type: STRING

SELECT HEX('ab'), HEX(128), CONV(HEX(128), 16, 10);
HEX('ab') HEX(128) CONV(HEX(128), 16, 10)
6162 80 128

LN ( x )

The LN function returns the natural log value (base = e) of an antilogarithm x. The return value is a DOUBLE type. If the antilogarithm is 0 or a negative number, an error is returned. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a positive number

Return type: DOUBLE

SELECT ln(1), ln(2.72);
ln(1) ln(2.72)
0 1.000631880307906
LOG2 ( x )

The LOG2 function returns a log value whose antilogarithm is x and base is 2. The return value is a DOUBLE type. If the antilogarithm is 0 or a negative number, an error is returned. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a positive number

Return type: DOUBLE

SELECT log2(1), log2(8);
log2(1) log2(8)
0 3
LOG10 ( x )

The LOG10 function returns the common log value of an antilogarithm x. The return value is a DOUBLE type. If the antilogarithm is 0 or a negative number, an error is returned. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a positive number

Return type: DOUBLE

SELECT log10(1), log10(1000);
log10(1) log10(1000)
0 3
MOD (m, n)

The MOD function returns the remainder of the first parameter m divided by the second parameter n. If n is 0, m is returned without the division operation being performed. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Note that if the dividend, the parameter m of the MOD function, is a negative number, the function operates differently from a typical operation (classical modulus) method.

Result of MOD

m n MOD(m, n) Classical modulus m-n*FLOOR(m/n)
11 4 3 3
11 -4 3 -1
-11 4 -3 1
-11 -4 -3 -3
11 0 11 Divided by 0 error

Parameters

  • m: Represents a dividend. It is an expression that returns a numeric value.
  • n: Represents a divisor. It is an expression that returns a numeric value.

Return type: INT

--it returns the reminder of m divided by n
SELECT MOD(11, 4), MOD(11, -4), MOD(-11, 4), MOD(-11, -4), MOD(11,0);
MOD(11, 4) MOD(11, -4) MOD(-11, 4) MOD(-11, -4) MOD(11,0)
3 3 -3 -3 11
SELECT MOD(11.0, 4), MOD(11.000, 4), MOD(11, 4.0), MOD(11, 4.000);
MOD(11.0, 4) MOD(11.000, 4) MOD(11, 4.0) MOD(11, 4.000)
3.0 3.000 3.0 3.000

PI ()

The PI function returns the ? value of type DOUBLE.

Return type: DOUBLE

SELECT PI(), PI()/2;
PI() PI()/2
3.141592653589793 1.570796326794896

POW( x, y )

POWER( x, y )

The POW function returns x to the power of y. The functions POW and POWER are used interchangeably. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: It represents the base. It is an expression that returns a numeric value. An expression that returns a numeric value.
  • y: It represents the exponent. An expression that returns a numeric value. If the base is a negative number, an integer must specified as the exponent.

Return type: DOUBLE

SELECT POWER(2, 5), POWER(-2, 5), POWER(0, 0), POWER(1,0);
POWER(2, 5) POWER(-2, 5) POWER(0, 0) POWER(1,0)
32 -32 1 1
--it returns an error when the negative base is powered by a non-int exponent
SELECT POWER(-2, -5.1), POWER(-2, -5.1);
ERROR: Argument of power() is out of range.
RADIANS ( x )

The RADIANS function returns the argument x specified in degrees converted to a radian value. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT RADIANS(90), RADIANS(180), RADIANS(360);
RADIANS(90) RADIANS(180) RADIANS(360)
1.570796326794896 3.141592653589793 6.283185307179586

RANDOM ( [seed] )

RAND ( [seed] )

The function RANDOM or RAND returns any integer value, which is greater than or equal to 0 and less than 2 31, and a seed argument that is INTEGER type can be specified. It rounds up real numbers and an error is returned when it exceeds the range of INTEGER.

When seed value is not given, the RAND function performs the operation only once to produce only one random number regardless of the number of rows where the operation is output, but the RANDOM function performs the operation every time the statement is repeated to produce a different random value for each row. Therefore, to output rows in a random order, you must use the RANDOM function.

To obtain a random real number, use the DRANDOM.

Parameters

  • seed:

Return type: INT

SELECT RAND(), RAND(1), RAND(1.4);
1526981144 89400484 89400484

The first value changes on every run; a seed makes the result repeatable.

RAND() is evaluated once per statement, while RANDOM() is evaluated for every row. Use RANDOM() to sort rows in random order:

SELECT id, name FROM product ORDER BY RANDOM() LIMIT 3;
ROUND ( number_operand, integer )

The ROUND function returns the specified argument, number_operand, rounded to the number of places after the decimal point specified by the integer. If the integer argument is a negative number, it rounds to a place before the decimal point, that is, at the integer part. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number_operand: An expression that returns a numeric value
  • integer: Specifies the place to round to. If a positive integer n is specified, the number is represented to the nth place after the decimal point; if a negative integer n is specified, the number is rounded to the n th place before the decimal point.

Return type: same type as the number_operand

--it rounds a number to one decimal point when the second argument is omitted
SELECT ROUND(34567.34567), ROUND(-34567.34567);
ROUND(34567.34567) ROUND(-34567.34567)
34567.00000 -34567.00000
--it rounds a number to three decimal point
SELECT ROUND(34567.34567, 3), ROUND(-34567.34567, 3) FROM db_root;
ROUND(34567.34567, 3) ROUND(-34567.34567, 3)
34567.34600 -34567.34600
--it rounds a number three digit to the left of the decimal point
SELECT ROUND(34567.34567, -3), ROUND(-34567.34567, -3);
ROUND(34567.34567, -3) ROUND(-34567.34567, -3)
35000.00000 -35000.00000
SIGN (number_operand)

The SIGN function returns the sign of a given number. It returns 1 for a positive value, -1 for a negative value, and 0 for zero. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • number_operand: An operator which returns a numeric value

Return type: INT

--it returns the sign of the argument
SELECT SIGN(12.3), SIGN(-12.3), SIGN(0);
SIGN(12.3) SIGN(-12.3) SIGN(0)
1 -1 0
SIN ( x )

The SIN function returns a sine value of the parameter. The argument x must be a radian value. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT SIN(pi()/6), SIN(pi()/3), SIN(pi());
SIN(pi()/6) SIN(pi()/3) SIN(pi())
0.4999999999999999 0.8660254037844386 1.224646799147353e-16
SQRT ( x )

The SQRT function returns the square root of x as a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value. An error is returned if this value is a negative number.

Return type: DOUBLE

SELECT SQRT(4), SQRT(16.0);
SQRT(4) SQRT(16.0)
2 4
TAN ( x )

The TAN function returns a tangent value of the argument. The argument x must be a radian value. The return value is a DOUBLE type. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value

Return type: DOUBLE

SELECT TAN(pi()/6), TAN(pi()/3), TAN(pi()/4);
TAN(pi()/6) TAN(pi()/3) TAN(pi()/4)
0.5773502691896257 1.732050807568876 0.9999999999999999
TRUNC ( x[, dec] )

TRUNCATE ( x, dec )

The function TRUNC or TRUNCATE truncates the numbers of the specified argument x to the right of the dec position. If the dec argument is a negative number, it displays 0s to the dec- th position left to the decimal point. Note that the dec argument of the TRUNC function can be omitted, but that of the TRUNCATE function cannot be omitted. If the dec argument is a negative number, it displays 0s to the dec -th position left to the decimal point. The number of digits of the return value to be represented follows the argument x. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

Parameters

  • x: An expression that returns a numeric value
  • dec: The place to be truncated is specified. If a positive integer n is specified, the number is represented to the *n-*th place after the decimal point; if a negative integer n is specified, the number is truncated to the *n-*th place before the decimal point. It truncates to the first place after the decimal point if the dec argument is 0 or omitted. Note that the dec argument cannot be omitted in the TRUNCATE function.

Return type: same type as the x

--it returns a number truncated to 0 places
SELECT TRUNC(34567.34567), TRUNCATE(34567.34567, 0);
TRUNC(34567.34567) TRUNCATE(34567.34567, 0)
34567.00000 34567.00000
--it returns a number truncated to three decimal places
SELECT TRUNC(34567.34567, 3), TRUNC(-34567.34567, 3);
TRUNC(34567.34567, 3) TRUNC(-34567.34567, 3)
34567.34500 -34567.34500
--it returns a number truncated to three digits left of the decimal point
SELECT TRUNC(34567.34567, -3), TRUNC(-34567.34567, -3);
TRUNC(34567.34567, -3) TRUNC(-34567.34567, -3)
34000.00000 -34000.00000
WIDTH_BUCKET(expression, from, to, num_buckets)

WIDTH_BUCKET distributes the rows in an ordered partition into a specified number of buckets. The buckets are numbered, starting from one. That is, WIDTH_BUCKET function creates an equi-width histogram. The return value is an integer. When you input the string which cannot be transformed into the number, it returns an error if the value of return_null_on_function_errors in coradb.conf is no(the default), or returns NULL if it is yes.

This function equally divides the range by the given number of buckets and assigns the bucket number to each bucket. That is, every interval (bucket) has the identical size.

Note that NTILE function equally divides the number of rows by the given number of buckets and assigns the bucket number to each bucket. That is, every bucket has the same number of rows.

expression is an input value to assign the bucket number. from and to should be numeric values, date/time values, or the string which can be converted to date/time value. from is included in the acceptable range, but to is beyond the range.

For example, WIDTH_BUCKET(score, 80, 50, 3) (a descending range) returns

  • 0 when the score is larger than 80,
  • 1 for [80, 70),
  • 2 for [70, 60),
  • 3 for [60, 50),
  • and 4 when the score is 50 or smaller.

Parameters

  • expression: an input value to assign the bucket number. It specifies a certain expression which returns the number.
  • from: a start value of the range, which is given to expression. It is included in the entire range.
  • to: an end value of the range, which is given to expression. It is not included in the entire range.
  • num_buckets: the number of buckets. The #0 bucket and the #(num_buckets + 1) bucket are created to include the contents beyond the range.

Return type: INT

The following example puts products into four price bands of 25.00 each between 0 and 100. Prices of 100 or more go to bucket 5 (num_buckets + 1):

SELECT name, unit_price, WIDTH_BUCKET(unit_price, 0, 100, 4) AS price_band
FROM product
WHERE id IN (5, 29, 38, 123, 335, 1110, 1795)
ORDER BY unit_price DESC;
name unit_price price_band
Côte de Blaye 263.50 5
Thüringer Rostbratwurst 123.79 5
Ingrid Running Jacket 84.00 4
Erika Running Short 45.00 2
Balboa Persistence Tee 29.00 2
Chef Antons Gumbo Mix 21.35 1
Arcadio Gym Short 20.00 1

The range can also be made of dates. The year 2025 is split into four equal parts (of about 91 days, so the boundaries are not calendar quarters); orders from December 2024 fall before the range, in bucket 0:

SELECT WIDTH_BUCKET(order_date, DATE'2025-01-01', DATE'2026-01-01', 4) AS bucket,
MIN(order_date) AS first_day, MAX(order_date) AS last_day, COUNT(*) AS orders
FROM orders
WHERE order_date >= DATE'2024-12-01'
GROUP BY 1
ORDER BY 1;
bucket first_day last_day orders
0 2024-12-01 2024-12-31 94
1 2025-01-01 2025-04-02 150
2 2025-04-04 2025-07-02 173
3 2025-07-03 2025-10-01 183
4 2025-10-02 2025-12-31 280