abs()

The abs() function returns the absolute value of a numeric expression.

Syntax

abs(NUM_EXPR)

Parameters

NUM_EXPR
A numeric expression whose absolute value is to be computed.

Description

The abs() function converts negative input to a positive value and returns zero or positive input unchanged. The return type matches the input type: a 32-bit integer input returns a 32-bit integer, a 64-bit integer returns a 64-bit integer, and a 32-bit float or 64-bit double returns the corresponding floating-point type.

Returns null if the input is null or not a number (including boolean).

Error codes

Error codeDescription
90600The number of parameters is not exactly 1.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Apply absolute value to the difference between request size and a reference value.

    table limit=5 WEB_APACHE_SAMPLE | eval result = abs(bytes - 1000)
    | fields bytes, result
    
  2. NULL input

    json "{'val': null}" | eval result = abs(val)
    | # result: null
    

Compatibility

The abs() function is available since before Sonar 4.0.