For the complete documentation index, see llms.txt. This page is also available as Markdown.

string_count

Use the string_count() function to count the number of times a specified substring (pattern) appears within a given string.

Syntax

string_count (<string>, <pattern>)

Parameters

Name
Type
Required
Description

string

string

Yes

The input string field or literal value in which you want to search.

pattern

string

Yes

The substring literal that you want to count.

Returns

The string_count() function returns an integer representing the number of occurrences found.

Usage notes

  • By default, XQL queries operate with case-sensitivity unless config case_sensitive = false is explicitly set. This also applies to string_count().

  • If the input string value is NULL, the string_count() function will return NULL.

  • If the pattern does not appear in the string, the function returns 0.

Examples

Example 1: Counting occurrences of a specific character in a string field

Goal: Count the occurrences of the character 'o' in the event_description field.

XQL code:

Explanation: The query counts how many times the character 'o' appears in the event_description for each specified event.

Output:

EVENT_ID
EVENT_DESCRIPTION
COUNT_OF_O

101

"User login successful"

1

102

"File access attempt"

0

103

"Network connection established"

3

Example 2: Counting occurrences of a substring (word) in a string field

Goal: Count the occurrences of the word "access" in the raw_log_data field.

XQL code:

Explanation: The string_count() function correctly identifies and counts the single occurrence of "access" in event ID 102's raw_log_data.

Output:

EVENT_ID
RAW_LOG_DATA
COUNT_OF_ACCESS

101

"User Alice logged in from 192.168.1.10"

0

102

"Process cmd.exe attempted to access /etc/passwd"

1

Example 3: Counting delimiters in an IP address string

Goal: Count the number of dot (.) delimiters within ipv4_address strings.

XQL code:

Explanation: The query accurately counts the three dot separators in each IPv4 address string.

Output:

EVENT_ID
IPV4_ADDRESS
COUNT_OF_DOTS

101

"192.168.1.10"

3

102

"10.0.0.5"

3

Example 4: Case-insensitive counting (default behavior)

Goal: Demonstrate string_count()'s default case-sensitive behavior by searching for "user" (lowercase) in a field containing "User" (uppercase).

XQL code:

Explanation: The pattern "user" (lowercase) does not successfully match "User" in the raw_log_data due to the case-sensitive behavior of XQL.

Output:

EVENT_ID
RAW_LOG_DATA
COUNT_USER_LOWERCASE_SEARCH

101

"User Alice logged in from 192.168.1.10"

0

Example 5: Case-sensitive counting (with config case_sensitive = false)

Goal: Explicitly set case sensitivity to false to show how it affects string_count() results when searching for "user" vs. "User".

XQL code:

Explanation: When config case_sensitive is false, string_count() does not distinguish between "User" and "user", returning 1 for both cases.

Output:

EVENT_ID
RAW_LOG_DATA
COUNT_USER_UPPERCASE_SEARCH
COUNT_USER_LOWERCASE_SEARCH

101

"User Alice logged in from 192.168.1.10"

1

1

Example 6: Counting a pattern not present in the string

Goal: Demonstrate the result when the specified pattern does not exist in the input string.

XQL code:

Explanation: As expected, string_count() returns 0 when the nonexistent_word pattern is not found in the event_description.

Output:

EVENT_ID
EVENT_DESCRIPTION
COUNT_OF_NONEXISTENT_PATTERN

101

"User login successful"

0

Example 7: Handling NULL input string

Goal: Show how string_count() behaves when the input string field contains a NULL value.

XQL code:

Explanation: Consistent with XQL function behavior, if the input string (dst_domain for event ID 105) is NULL, the string_count() function returns NULL.

Output:

EVENT_ID
DST_DOMAIN
COUNT_IN_NULL_DOMAIN

105

NULL

NULL

Last updated

Was this helpful?