long()

Converts a value to a 64-bit integer.

Syntax

long(EXPR)

Parameters

EXPR
The value or expression to convert to a 64-bit integer.

Description

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

Behavior by input type:

  • Numeric: Calls longValue() for conversion. Returns null for a float or double outside the 64-bit integer range.
  • IPv4 address: Converts the 4 bytes to an unsigned integer. For example, 192.0.2.1 returns 3221225985.
  • 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
90840A 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 src_ip field to a 64-bit integer

    table limit=5 WEB_APACHE_SAMPLE | eval result = long(src_ip) | fields src_ip, result
    | # result: the IP address converted to an unsigned integer
    
  2. Converting the bytes field to a 64-bit integer

    table limit=5 WEB_APACHE_SAMPLE | eval result = long(bytes) | fields bytes, result
    | # result: the same 64-bit integer value as the bytes field
    
  3. Non-convertible string

    json "{'val': 'invalid'}" | eval result = long(val)
    | # result: null
    
  4. NULL input

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

Compatibility

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