The PostgreSQL ABS()
function returns the absolute value of a number.
Syntax
The following illustrates the syntax of the ABS()
function:
1 | 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.
Examples
The following example shows how to use the ABS()
function to calculate the absolute value of a number:
1 | SELECT ABS(-10.25) |
The result is:
1 | 10.25 |
The following statement uses an expression for the ABS()
function:
1 | SELECT ABS( 100 - 250 ); |
Here is the result:
1 | 150 |
Besides the ABS()
function, you can use the absolute operator @, for example:
1 | SELECT @ -15 |
It returned 15 as expected.
1 | -15 |
In this tutorial, you have learned how to use the PostgreSQL ABS()
function to calculate the absolute value of a number.