to_number
Use the to_number() function to convert a string value that represents a number or a numeric data type into a floating-point number.
Syntax
to_number (<string>)Parameters
string
string
Yes
The string field or literal value that you wish to convert to a number.
Returns
The to_number() function returns a floating-point number.
Usage notes
The function requires a string value that represents a number or a numeric data type.
The function is functionally identical to the
to_float()function.The function consistently returns a floating-point number. Even if the input string represents a whole integer (for example, "200"), the output will be a floating-point number (for example, 200.0).
If the input string cannot be successfully converted to a numeric value (for example, it contains letters or unparseable characters), the function will return
NULL.If the input string itself is
NULL, the function will returnNULL.
Examples
Example 1: Converting a literal integer string
Goal: Convert a string literal representing an integer into a numerical (float) value.
XQL code:
Explanation: The literal string "42" is successfully converted by to_number() into a floating-point number 42.0.
Output:
101
42.0
102
42.0
103
42.0
Example 2: Converting a literal floating-point string
Goal: Convert a string literal representing a decimal number into a floating-point value.
XQL code:
Explanation: The literal string "123.45" is correctly converted into the floating-point number 123.45.
Output:
101
123.45
102
123.45
103
123.45
Example 3: Converting a numeric string extracted from a JSON field
Goal: Extract a numerical value from a JSON string field and convert it from its string representation to a number.
XQL code:
Explanation: For event_id 101, the "code" value "200" is extracted as a string and then successfully converted to the number 200.0. For event_id 102 and 103, where simple_json_data does not contain a "code" field, extracted_code_as_string becomes NULL, and subsequently, converted_json_code also becomes NULL.
Output:
101
{"status": "ok", "code": 200}
200.0
102
{"status": "fail", "error": "access_denied"}
NULL
103
{"connection_id": "CONN-001", "protocol": "TCP"}
NULL
Example 4: Attempting to convert a string with non-numeric characters
Goal: Demonstrate the behavior when attempting to convert a string that contains non-numerical characters.
XQL code:
Explanation: to_number() cannot perform the conversion and returns NULL for invalid inputs because "80%" is not a pure numeric string.
Output:
104
{"system": {"cpu_util": 0.15, "mem_free": "80%"}, "status": "active"}
NULL
Example 5: Converting a NULL input field
Goal: Demonstrate the behavior when the input field is explicitly NULL.
XQL code:
Explanation: When the input to to_number() is NULL, the function consistently returns NULL.
Output:
105
NULL
NULL
Related articles
Functions:
to_float,to_integer,json_extract_scalar
Last updated
Was this helpful?
