Overview

The ABS() function returns an absolute number, i.e., the positive value of a number. The data type of the returned value will depend on the data type of the value passed to the ABS() function.

Syntax

The syntax for the ABS() function is as follows:

ABS(x)

The ABS() function requires one argument:

  • x: An expression that evaluates to a number.
The ABS() function will return the negation of the negative numbers.

Examples

Case #1: Absolute value of a negative number

The following example demonstrates how the ABS() function can be used to obtain the absolute value of a negative number:

SELECT ABS(-10.25);

It will return an absolute value of the passed argument:

+--------+
| f      | 
+--------+
| 10.25  |
+--------+

Case #2: ABS() function with an expression

The following example demonstrates how the ABS() function can be used with an expression to obtain the absolute value of the result:

SELECT ABS( 100 - 250);

The result of the above statement is -150. However, you will get the output 150, as 150 is the positive version of -150.

+------+
| f    | 
+------+
| 150  |
+------+

Case #3: Using the ABS() function with a table

The following example demonstrates how the ABS() function can be used with a table to obtain the absolute values of all numbers in a specific column:

  1. First, create a table named absTable containing an initialValue column with some positive and negative values:
CREATE TABLE absTable(initialValue float);

INSERT INTO absTable(initialValue)
VALUES 
(550),
(-210), 
(72.12),
(-87.93),
(-0.0);
  1. Next, use the following query to find the absolute value of all numbers:
SELECT initialValue, ABS(initialValue) AS absoluteValue
FROM absTable;
  1. The above query will retrieve all values in the “initialValue” column and their absolute values in the “absoluteValue” column. The output will look something like this:
+---------------+----------------+
| initialValue  | absoluteValue  |
+---------------+----------------+
| 550           | 550            |
| -210          | 210            |
| 72.12         | 72.12          |
| 87.93         | 87.93          |
| -0            | 0              |
+---------------+----------------+