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 STR is null, the function returns null.
  • If STR is shorter than LENGTH, the entire STR is returned.
  • If LENGTH is 0, an empty string ("") is returned.
  • If LENGTH is negative, the function returns null.
  • If LENGTH is specified as a negative constant, a query parsing error (error code 90721) is raised.

Error codes

Error codeDescription
90721A negative constant was specified for LENGTH.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. 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
    
  2. 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.
    
  3. NULL input

    json "{'val': null}" | eval result = right(val, 3)
    | # result: null
    

Compatibility

The right() function is available since before Sonar 4.0.