The CONCAT function concatenates multiple strings in sequence into one string. This function supports a variable number of arguments and is one of the most basic and commonly used functions in string processing. It is widely used in scenarios such as data concatenation, report generation, and dynamic SQL construction. Note that if any argument is NULL, the entire result will be NULL.
CONCAT(<expr> [, <expr> ...])
| Parameter | Description |
|---|---|
<expr> | String expression to be concatenated, can be a string constant, column name, or other expression. Type: VARCHAR |
Returns VARCHAR type, representing the concatenated string of all arguments.
Concatenation rules:
Special cases:
SELECT CONCAT('a', 'b'), CONCAT('a', 'b', 'c');
+------------------+-----------------------+ | CONCAT('a', 'b') | CONCAT('a', 'b', 'c') | +------------------+-----------------------+ | ab | abc | +------------------+-----------------------+
SELECT CONCAT('a', NULL, 'c'), CONCAT('hello', NULL);
+------------------------+---------------------+ | CONCAT('a', NULL, 'c') | CONCAT('hello', NULL) | +------------------------+---------------------+ | NULL | NULL | +------------------------+---------------------+
SELECT CONCAT('hello', '', 'world'), CONCAT('', 'test', '');
+-----------------------------+------------------------+ | CONCAT('hello', '', 'world') | CONCAT('', 'test', '') | +-----------------------------+------------------------+ | helloworld | test | +-----------------------------+------------------------+
SELECT CONCAT('User', 123), CONCAT('Price: $', 99.99);
+---------------------+---------------------------+ | CONCAT('User', 123) | CONCAT('Price: $', 99.99) | +---------------------+---------------------------+ | User123 | Price: $99.99 | +---------------------+---------------------------+
SELECT CONCAT('A', 'B', 'C', 'D', 'E'), CONCAT('1', '2', '3', '4', '5');
+----------------------------------+----------------------------------+ | CONCAT('A', 'B', 'C', 'D', 'E') | CONCAT('1', '2', '3', '4', '5') | +----------------------------------+----------------------------------+ | ABCDE | 12345 | +----------------------------------+----------------------------------+
SELECT CONCAT('ṭṛì', ' ', 'ḍḍumai'), CONCAT('Hello', ' ', 'ṭṛì', ' ', 'ḍḍumai');
+------------------------------+--------------------------------------+ | CONCAT('ṭṛì', ' ', 'ḍḍumai') | CONCAT('Hello', ' ', 'ṭṛì', ' ', 'ḍḍumai') | +------------------------------+--------------------------------------+ | ṭṛì ḍḍumai | Hello ṭṛì ḍḍumai | +------------------------------+--------------------------------------+
SELECT CONCAT('/home/', 'user/', 'file.txt'), CONCAT('https://', 'www.example.com', '/api');
+--------------------------------------+----------------------------------------------+ | CONCAT('/home/', 'user/', 'file.txt') | CONCAT('https://', 'www.example.com', '/api') | +--------------------------------------+----------------------------------------------+ | /home/user/file.txt | https://www.example.com/api | +--------------------------------------+----------------------------------------------+
SELECT CONCAT('user', '@', 'example.com'), CONCAT('admin.', 'support', '@', 'company.org');
+------------------------------------+-----------------------------------------------+ | CONCAT('user', '@', 'example.com') | CONCAT('admin.', 'support', '@', 'company.org') | +------------------------------------+-----------------------------------------------+ | user@example.com | admin.support@company.org | +------------------------------------+-----------------------------------------------+