What is the usage of the MySQL substring function?
The substring function in MySQL is used to extract a portion of a string.
Syntax:
SUBSTRING(str, start, length)
Parameters:
- String: the string to be extracted.
- Start: Specify the starting position for extraction, with counting starting from 1.
- A parameter called “length” is optional and specifies the number of characters to extract. If “length” is not specified, all characters from the “start” position to the end of the string are returned.
For example, if we have a string “Hello World”, we can use the substring function to extract a portion of it.
SELECT SUBSTRING(‘Hello World’, 7) AS ‘Output’; — Output: World
Return the substring ‘World’ starting from the 7th position in the string ‘Hello World’.
Output: Hello