In MariaDB, ABS()
is a built-in numeric function that returns the absolute (non-negative) value of its argument.
Syntax
The syntax goes like this:
ABS(X)
Where X
is the number in question. If X
is not a number, it is converted to a numeric type.
Example
Here’s an example:
SELECT ABS(300);
Result:
+----------+ | ABS(300) | +----------+ | 300 | +----------+
We get the same result if the number is negative:
SELECT ABS(-300);
Result:
+-----------+ | ABS(-300) | +-----------+ | 300 | +-----------+
Non-Numeric Argument
If the argument is not a number, it is converted to a numeric type.
Example:
SELECT ABS(DATE '2020-12-10');
Result:
+------------------------+ | ABS(DATE '2020-12-10') | +------------------------+ | 20201210 | +------------------------+
However, this depends on the argument/context.
Here’s what happens when I omit the DATE
keyword:
SELECT ABS('2020-12-10');
Result:
+-------------------+ | ABS('2020-12-10') | +-------------------+ | 2020 | +-------------------+ 1 row in set, 1 warning (0.000 sec)
Let’s check the warning:
SHOW WARNINGS;
Result:
+---------+------+------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: '2020-12-10' | +---------+------+------------------------------------------------+
Missing Argument
Calling ABS()
without an argument results in an error:
SELECT ABS();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'ABS'