MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL ABS() Function



The MySQL ABS() function returns the absolute value of a given number.

Syntax

ABS(number)

Parameters

number Required. Specify the number to convert to an absolute value.

Return Value

Returns the absolute value of a given number.

Example 1:

The example below shows the usage of ABS() function.

mysql> SELECT ABS(15);
Result: 15

mysql> SELECT ABS(-15);
Result: 15

mysql> SELECT ABS(25.23);
Result: 25.23

mysql> SELECT ABS(-25.23);
Result: 25.23

mysql> SELECT ABS(0);
Result: 0

mysql> SELECT ABS(10 * -2);
Result: 20

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 1-10
Data 2-5
Data 30
Data 45
Data 510

The query given below can be used to calculate the absolute value of column x.

SELECT *, ABS(x) AS ABS_Value FROM Sample;

This will produce the result as shown below:

DataxABS_Value
Data 1-1010
Data 2-55
Data 300
Data 455
Data 51010

❮ MySQL Functions

5