MariaDB ABS() Function

In MariaDB, ABS() is a built-in numeric function that returns the absolute value of a given number.

MariaDB ABS() Syntax

Here is the syntax of the MariaDB ABS() function:

ABS(number)

Parameters

number

Required. A number used to calculate the absolute value.

If you provide the wrong number of parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'ABS'.

Return value

The MariaDB ABS() function returns the absolute (positive) value of a number.

If the number parameter is a string, ABS() will try to convert it to a number according to the following rules:

  • If it starts with a number, convert the leading part of the number to a number.
  • If it cannot be converted to a number, MariaDB treats it as 0.

If the number parameter is NULL, the ABS() function will return NULL.

MariaDB ABS() Examples

The following statement shows the basic usage of the MariaDB ABS() function:

SELECT
  ABS(100),
  ABS(-100),
  ABS('100'),
  ABS('-100'),
  ABS('-100A'),
  ABS('A100'),
  ABS(NULL)\G

Output:

    ABS(100): 100
   ABS(-100): 100
  ABS('100'): 100
 ABS('-100'): 100
ABS('-100A'): 100
 ABS('A100'): 0
   ABS(NULL): NULL

Conclusion

In MariaDB, ABS() is a built-in numeric function that returns the absolute value of a given number.