Concept Flow - CONCAT and CONCAT_WS
Start with strings
Apply CONCAT or CONCAT_WS
Check each argument
Join strings
Return combined string
The functions take strings, check each for NULL, then join them into one string, returning the combined result.
SELECT CONCAT('Hello', ' ', 'World'); SELECT CONCAT_WS('-', '2024', NULL, '06', '15');
| Step | Function | Arguments | NULL Handling | Result |
|---|---|---|---|---|
| 1 | CONCAT | 'Hello', ' ', 'World' | No NULLs, all included | 'Hello World' |
| 2 | CONCAT_WS | '-', '2024', NULL, '06', '15' | NULL skipped | '2024-06-15' |
| 3 | CONCAT | 'A', NULL, 'B' | NULL treated as empty string | 'AB' |
| 4 | CONCAT_WS | ',' , NULL, 'X', NULL, 'Y' | NULL skipped | 'X,Y' |
| 5 | CONCAT_WS | NULL, 'X', 'Y' | Separator NULL returns NULL | NULL |
| 6 | CONCAT | NULL, NULL | NULL treated as empty strings | '' |
| 7 | CONCAT_WS | '-', NULL, NULL | All NULLs skipped, no strings | '' |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 | After Step 6 | After Step 7 |
|---|---|---|---|---|---|---|---|---|
| Result String | '' | 'Hello World' | '2024-06-15' | 'AB' | 'X,Y' | NULL | '' | '' |
CONCAT(str1, str2, ...) joins all strings, treating NULL as empty.
CONCAT_WS(separator, str1, str2, ...) joins strings with separator, skipping NULLs.
If separator is NULL in CONCAT_WS, result is NULL.
Useful for combining columns or literals into one string.
NULL handling differs: CONCAT includes empty, CONCAT_WS skips.
Syntax: CONCAT('a', NULL, 'b') -> 'ab'; CONCAT_WS('-', 'a', NULL, 'b') -> 'a-b'.