right()
The right() function returns a substring of the specified length taken from the right end of a string.
Syntax
right(STR, LENGTH)
Parameters
STR- The original string field or value. Non-string values are converted to a string before processing.
LENGTH- The number of characters to extract from the right. Must be a non-negative integer. Negative values are not allowed when specified as a constant.
Description
The right() function returns a substring consisting of LENGTH characters from the right end of STR.
- If
STRisnull, the function returnsnull. - If
STRis shorter thanLENGTH, the entireSTRis returned. - If
LENGTHis0, an empty string ("") is returned. - If
LENGTHis negative, the function returnsnull. - If
LENGTHis specified as a negative constant, a query parsing error (error code90721) is raised.
Error codes
| Error code | Description |
|---|---|
| 90721 | A negative constant was specified for LENGTH. |
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Extract the last 5 characters of a URI (useful for checking file extensions)
table limit=5 WEB_APACHE_SAMPLE | eval result = right(uri, 5) | fields uri, result -
Extract the HTTP protocol version number (last 3 characters)
table limit=5 WEB_APACHE_SAMPLE | eval result = right(protocol, 3) | fields protocol, result | # result: "1.1", "2.0", etc. -
NULL input
json "{'val': null}" | eval result = right(val, 3) | # result: null
Compatibility
The right() function is available since before Sonar 4.0.