int()

Converts a value to a 32-bit integer.

Syntax

int(EXPR)

Parameters

EXPR
The value or expression to convert to an integer.

Description

The int() function converts the argument to a 32-bit integer and returns it. If the argument is null, returns null. If conversion fails, also returns null.

Behavior by input type:

  • Numeric: Calls intValue() for conversion. Returns null if the value is outside the 32-bit integer range (-2,147,483,648 to 2,147,483,647).
  • IPv4 address: Converts the 4 bytes to a big-endian signed integer.
  • String: Parsed as a decimal number. Returns null for an empty string.
  • Array or list: The conversion is applied recursively to each element.

Only base 10 (decimal) is currently supported. Specifying a different base causes an error during the parsing phase.

Error codes

Error codeDescription
90830A base other than 10 was specified.

Usage examples

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

  1. Converting the HTTP status code to a string and then back to an integer

    table limit=5 WEB_APACHE_SAMPLE | eval result = int(string(status)) | fields status, result
    | # result: the same integer value as the status field
    
  2. Converting a floating-point number to an integer (truncates the decimal part)

    json "{'val': 12345.6789}" | eval result = int(val)
    | # result: 12345
    
  3. Converting an IPv4 address to an integer

    json "{'val': '192.0.2.1'}" | eval result = int(ip(val))
    | # result: -1073741311
    
  4. NULL input

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

Compatibility

The int() function has been available since before Sonar 4.0.