MySQL ABS() Function

The ABS() function returns the absolute (non-negative) value of a numeric value, effectively removing any negative sign.


Syntax

ABS(number)

Definition and Usage

  • The ABS() function calculates the absolute value of a given number.
  • It always returns a positive value or zero, depending on the input.

Parameter Values

Parameter Description
number Required. The numeric value to evaluate.

Technical Details

  • Works in: From MySQL 4.0

Examples

Example 1: Absolute value of a negative number

SELECT ABS(-243.5);

Result:

243.5

Example 2: Absolute value of a positive number

SELECT ABS(15);

Result:

15

Example 3: Absolute value of zero

SELECT ABS(0);

Result:

0

Example 4: Using ABS() in a query

Suppose we have a table Numbers:

Value
-10
5
-3.4

Query:

SELECT Value, ABS(Value) AS AbsoluteValue
FROM Numbers;

Result:

Value AbsoluteValue
-10 10
5 5
-3.4 3.4

Usage Notes

  • This function is particularly useful for mathematical calculations, ensuring all results are positive when needed.
  • It can be used in conjunction with other mathematical functions for data processing and analysis.

Was this article helpful?