PostgreSQL ABS() Function

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

Syntax

The following illustrates the syntax of the ABS() function:

ABS(numeric_expression)

Arguments

The ABS() function requires one argument:

1) numeric_expression

The numeric_expression can be a number or a numeric expression that evaluates to a number.

Return Value

The ABS() function returns a value whose data type is the same as the input argument.

Absolute Operator @

Besides the ABS() function, you can use the absolute operator @:

@ expression

In this syntax, the @ operator returns the absolute value of the expression.

Examples

The following example shows how to use the ABS() function to calculate the absolute value of a number:

SELECT ABS(-10.25) result;Code language: CSS (css)

The result is:

 result
--------
  10.25
(1 row)
Code language: CSS (css)

The following statement uses an expression for the ABS() function:

SELECT ABS( 100 - 250 ) result;

Here is the result:

 result
--------
    150
(1 row)

Besides the ABS() function, you can use the absolute operator @, for example:

SELECT @ -15 as resultCode language: JavaScript (javascript)

It returned 15 as expected.

 result
--------
     15
(1 row)

In this tutorial, you have learned how to use the PostgreSQL ABS() function to calculate the absolute value of a number.

Was this tutorial helpful ?