Oracle ABS() Function

Oracle ABS() is a built-in function that returns the absolute value of the given parameter.

Oracle ABS() syntax

Here is the syntax for the Oracle ABS() function:

ABS(num)

Parameters

num

Required. It can be any numeric data type or any non-numeric data type that can be implicitly converted to a numeric data type.

Return Value

The Oracle ABS() function returns the absolute value of the given parameter. The function returns the same data type as the parameter’s numeric data type.

If the parameter cannot be converted to a number, ABS() will report an error.

If any parameter is NULL, ABS() will return NULL.

Oracle ABS() Examples

Here are some examples that demonstrate the usage of the Oracle ABS() function.

Basic Usage

SELECT
  ABS(100),
  ABS(-100),
  ABS('100'),
  ABS('-100')
FROM dual;

Output:

   ABS(100)    ABS(-100)    ABS('100')    ABS('-100')
___________ ____________ _____________ ______________
        100          100           100            100

Non-Numeric Values

If the parameter cannot be converted to a number, ABS() will give an error.

SELECT
  ABS('100A'),
  ABS('A100'),
  ABS('ABC')
FROM dual;

Output:

01722\. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.

NULL Parameters

If any parameter is NULL, ABS() will return NULL.

SET NULL 'NULL';
SELECT
    ABS(NULL)
FROM dual;

Output:

   ABS(NULL)
____________
        NULL

In this example, we use the statement SET NULL 'NULL'; to display NULL values as the string 'NULL'.

Conclusion

Oracle ABS() is a built-in function that returns the absolute value of the given parameter.