In MySQL, the CONCAT, CONCAT_WS, and SUBSTRING functions are used for string manipulation. Here’s an explanation of each function with some examples:
1. **CONCAT function:** The ‘CONCAT‘ function is used to concatenate two or more strings into a single string. It takes one or more string arguments and returns a string.
Syntax:
CONCAT(string1, string2, ...)
Example:
SELECT CONCAT('Hello', ' ', 'World');
Output:
'Hello World'
2. **CONCAT_WS function:** The ‘CONCAT_WS‘ function is used to concatenate strings with a separator. It takes a separator and two or more string arguments and returns a string with the separator between each of the strings.
Syntax:
CONCAT_WS(separator, string1, string2, ...)
Example:
SELECT CONCAT_WS(',', 'John', 'Doe', '25', 'Male');
Output:
'John,Doe,25,Male'
3. **SUBSTRING function:** The ‘SUBSTRING‘ function is used to extract a substring from a string. It takes a string argument and two integer arguments that represent the starting position and the length of the substring.
Syntax:
SUBSTRING(string, start, length)
Example:
SELECT SUBSTRING('Hello World', 7, 5);
Output:
'World'
These functions are useful for manipulating strings in MySQL, whether you need to join strings together, add separators between strings, or extract a substring from a larger string.