How is the substring function used in DB2?
In DB2, the SUBSTRING function is used to extract a substring from a string. Its syntax is as follows:
Extract a portion of a string starting from a specific position for a specified length.
- The source string from which the substring is to be extracted is the string-expression.
- Starting position: The initial position of the substring to be extracted, with indexing beginning at 1.
- The length of the substring to extract.
“Can you please turn off the lights before you leave?”
–> “Could you please switch off the lights before you go?”
Retrieve the characters ‘World’ from the string ‘Hello World’.
The above example will return ‘World’ by extracting 5 characters starting from the 7th character of the string ‘Hello World’.
Please note that in DB2 there is a similar function called SUBSTR, which has the same functionality as the SUBSTRING function, but with slightly different syntax.