How to use the SUBSTR function to extract part of a string?
The SUBSTR function is a function used for extracting parts of a string, allowing you to specify the starting position and length for the extraction.
The syntax for using the SUBSTR function is as follows: SUBSTR(string, starting position, length)
The specific operation steps are as follows:
- Specify the string to be truncated: the string to be truncated should be passed as the first parameter.
- Specify the starting position: Determine the starting point for the excerpt, with counting starting from 1 as the second parameter.
- Specify the length to be extracted: Determine the length to be extracted as the third parameter.
For example, let’s say we have a string “Hello, World!” and we want to extract the “World” part from it.
Here is the code that uses the SUBSTR function:
SELECT SUBSTR(‘Hello, World!’, 8, 5);
After running this code, the outcome will be “World”.
In this example, we specified the string “Hello, World!” with a starting position of 8 (counting from 1) and a length of 5. So, the SUBSTR function will extract the 5 characters starting from the 8th position, which is the “World” part.