abs
The abs() function returns an absolute number (for example, 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
Here’s the syntax for the `abs()`function:
ABS(x)
The abs() function requires one argument:
-
x: An expression that evaluates to a number.
|
The |
Examples
Absolute value of a negative number
This example demonstrates how to use the abs() function to obtain the absolute value of a negative number:
SELECT ABS(-10.25);
This returns an absolute value of the passed argument:
+--------+
| f |
+--------+
| 10.25 |
+--------+
abs() function with an expression
This example demonstrates how to use the abs() function with an expression to obtain the absolute value of the result:
SELECT ABS( 100 - 250);
The result of this statement is -150. However, the output is 150, as 150 is the positive version of -150.
+------+
| f |
+------+
| 150 |
+------+
Use the abs() function with a table
This example demonstrates how to use the abs() function with a table to obtain the absolute values of all numbers in a specific column:
-
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); -
Next, use this query to find the absolute value of all numbers:
SELECT initialValue, ABS(initialValue) AS absoluteValue FROM absTable; -
This query retrieves 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 | +---------------+----------------+