Overview
The SUBSTR() function extracts a specific number of characters from a string.
Syntax
The syntax of the function is illustrated below:
2 Arguments
3 Arguments
Both syntaxes will have input and return of type string.
Start Position
The start_position is used as the starting position, specifying the part from where the substring is to be returned. It is written as an integer value.
Length
The length is used to determine the number of characters to be extracted*. *It can be one or more characters.
Examples
Case 1: SUBSTR() function with specified start_position & length
In this example, we will set the start_position with the first six characters and have five characters extracted:
The updated table is shown below:
Case 2: SUBSTR() function with length = 0
The following query will extract a string with length = 0:
It will display an empty output as there is no length specified:
Case 3: SUBSTR() function with length = negative value
Here we will check if the length is specified with a negative value:
Instead of extracting the string from the last characters, it will return an error as seen below:
Case 4: SUBSTR() function with start_position > string
We know that Watermelon only has ten characters, but this time, we will figure out if the specified start_position is larger than the string’s characters:
It will display an empty output as shown below:
Case 5: SUBSTR() Function with 2 Arguments
In this example, we will set the start_position with the first six characters and have five characters extracted.
It will display the substring from position 6 output as shown below: