substr()
The substr() function returns the substring of a string corresponding to the specified index range.
Syntax
Parameters
STR- The original string field or value. Non-string values are converted to a string before processing.
BEGIN- The character index at which extraction starts. Indices start at 0. A negative value is counted backward from the end of the string. How an invalid
BEGINis handled differs by series — see Description below. If the converted start index is greater than or equal to the string length, or is less than 0,nullis returned. END- (Optional) The character index at which extraction ends (exclusive). Indices start at 0. If omitted, extraction continues to the end of the string. A negative value is counted backward from the end of the string. If
ENDis greater than the string length, extraction continues to the end of the string. How an invalidENDis handled differs by series — see Description below. IfBEGINis greater thanEND, orENDis less than 0 after conversion,nullis returned.
Description
The substr() function returns the substring of STR from the BEGIN index (inclusive) to the END index (exclusive).
- If
STRisnull, the function returnsnull. - If
ENDequalsBEGIN, an empty string ("") is returned. - How an invalid
BEGINorENDvalue is handled differs by series:- In the 5.0 series (through 5.0.2605.2): if
BEGINisnull, the entireSTRis returned. IfBEGINorENDcannot be interpreted as a number, the query fails with an error and processing of the remaining rows is aborted. - In the 4.0 series starting with 4.0.2609.0: if
BEGINorENDisnull, cannot be interpreted as a number, or is outside theintrange,nullis returned for that row only, and the query continues processing the remaining rows. A numeric string (for example,"42") is treated as a valid index.
- In the 5.0 series (through 5.0.2605.2): if
Error codes
N/A
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Extract the first 10 characters of a URI
table limit=5 WEB_APACHE_SAMPLE | eval result = substr(uri, 0, 10) | fields uri, result -
Extract the part of a URI after the first
/by specifying only the start indextable limit=5 WEB_APACHE_SAMPLE | eval result = substr(uri, 1) | fields uri, result | # result: "feed", "archives/2370", etc. -
Extract the end of a URI using a negative index
table limit=5 WEB_APACHE_SAMPLE | eval result = substr(uri, -5) | fields uri, result -
NULL input
json "{'val': null}" | eval result = substr(val, 0, 3) | # result: null -
NULL start index — result differs by series
json "{'val': 'hello'}" | eval result = substr(val, null) | # 5.0 series (through 5.0.2605.2) result: hello / 4.0 series (4.0.2609.0 and later) result: nullIn the 5.0 series (through 5.0.2605.2), if
BEGINisnull, the entire original string (val) is returned as-is. In the 4.0 series starting with 4.0.2609.0,nullis returned instead. -
Start index that cannot be interpreted as a number — result differs by series
json "{'val': 'hello'}" | eval result = substr(val, "abc")In the 5.0 series (through 5.0.2605.2), this kind of input raises an error that aborts the entire query. In the 4.0 series starting with 4.0.2609.0, only that row returns
null(result: null), and the remaining rows are processed normally.
Change history
| Target | Introduced in |
|---|---|
The substr() function | Before Sonar 4.0 |
Returning null for the affected row instead of aborting the query on an invalid position argument | 4.0.2609.0 |