to_integer
Use the to_integer() function to convert a string representation of a number into an integer value.
Syntax
to_integer(<string>)Parameters
string
string
Yes
The string field or literal value that you wish to convert to a number.
Returns
The to_integer() function returns an integer.
Usage notes
When a string representation of a floating-point number (for example, "1.5") is provided as input, the function returns
NULL. This differentiates its behavior from functions likefloor()orround().If passed a numeric data type (like a float), the result will be rounded up or down to the nearest integer.
If the input string does not represent a valid number, or if the input itself is
NULL, the function returnsNULL.
Examples
Example 1: Converting a literal integer string to integer
Goal: Convert a string literal that represents a whole number directly into an integer.
XQL code:
Explanation: The literal string "42" is successfully converted by to_integer() into the integer value 42.
Output:
101
42
102
42
103
42
Example 2: Converting a literal floating-point string to integer
Goal: Demonstrate behavior when provided with a string that represents a floating-point number.
XQL code:
Explanation: When a string containing a decimal (floating-point representation) is passed to to_integer(), it results in a NULL output.
Output:
101
NULL
102
NULL
103
NULL
Example 3: Converting a numeric string from an existing field to integer
Goal: Convert an existing floating-point field to an integer, demonstrating automatic rounding.
XQL code:
Explanation: The duration_seconds field is a floating-point number. When to_integer() is applied to a numeric type, it automatically rounds the number to the nearest integer (for example, 1.5 becomes 2, 0.8 becomes 1).
Output:
101
1.5
2
102
0.8
1
103
10.2
10
Example 4: Converting a string extracted from JSON to integer
Goal: Extract a numeric value from a JSON string field and convert it to an integer.
XQL code:
Explanation: For event 101, the "code" value "200" is extracted as a string and converted to the integer 200. For events where the field is missing, the result is NULL.
Output:
101
{"status": "ok", "code": 200}
"200"
200
102
{"status": "fail", "error": "access_denied"}
NULL
NULL
103
{"connection_id": "CONN-001", "protocol": "TCP"}
NULL
NULL
Example 5: Handling non-numeric string input
Goal: Demonstrate behavior when providing a string that cannot be interpreted as a number.
XQL code:
Explanation: The conversion returns NULL because event_description contains text (for example, "User login successful") and not a valid number.
Output:
101
"User login successful"
NULL
102
"File access attempt"
NULL
103
"Network connection established"
NULL
Example 6: Using to_integer() in a filter stage
Goal: Extract a numeric value from a string field, convert it to an integer, and use it for numerical comparison in a filter.
XQL code:
Explanation: The query splits the IP address to isolate the first octet as a string. to_integer() converts this string to an integer, allowing the filter stage to numerically compare if the octet is greater than 192.
Output:
106
"203.0.113.15"
"203"
203
Related articles
Functions:
to_string,to_number,to_float,split,arrayindex
Last updated
Was this helpful?
