left()

The left() function returns a substring of the specified length taken from the left side of a string.

Syntax

left(STR, LENGTH)

Parameters

STR
The string to extract from.
LENGTH
The number of characters to extract from the left. Must be 0 or greater. Negative values are not allowed when specified as a constant.

Description

The left() function returns LENGTH characters from the left side of STR. If STR is shorter than LENGTH, the entire STR is returned.

If STR is null, the function returns null. If STR is not a string, it is converted using toString() before processing.

If LENGTH is specified as a variable and its value is null, not an integer, or negative, the function returns null.

If LENGTH is specified as a negative constant, error code 90720 is raised and query parsing fails.

Error codes

90720
Raised when LENGTH is specified as a negative constant.

Usage examples

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

  1. Extract the first 10 characters of a URI

    table limit=5 WEB_APACHE_SAMPLE | eval result = left(uri, 10) | fields uri, result
    
  2. Extract the first 4 characters of the HTTP protocol version

    table limit=5 WEB_APACHE_SAMPLE | eval result = left(protocol, 4) | fields protocol, result
    | # result: "HTTP"
    
  3. NULL input

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

Compatibility

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