MariaDB CONCAT and CONCAT_WS: join strings and handle NULL
In MariaDB, CONCAT() joins its arguments directly, while CONCAT_WS() inserts its first argument as a separator between values. In ordinary SQL mode, a NULL argument makes CONCAT() return NULL. CONCAT_WS() skips NULL values after the separator but keeps empty strings, so separators can appear next to each other.
Join values with CONCAT
SELECT CONCAT('Maria', 'DB') AS product,
CONCAT('Kim', ' ', 'Minsu') AS full_name,
CONCAT('item-', CAST(42 AS CHAR)) AS item_label,
CONCAT('a', NULL, 'b') AS with_null;
Run the first SELECT without a table. product is MariaDB and item_label is item-42. The space in the name is another argument. with_null is NULL. Use CAST(… AS CHAR) when you want to make string conversion explicit. Binary arguments can make the resulting string binary. MariaDB Docs · MariaDB CONCAT()
The examples assume ordinary mode, with Oracle compatibility and EMPTY_STRING_IS_NULL disabled. Oracle mode changes CONCAT() to skip NULL, and EMPTY_STRING_IS_NULL makes an empty string literal behave as NULL. If results differ, inspect the current connection with SELECT @@session.sql_mode. MariaDB Docs · MariaDB SQL_MODE
Compare separators, NULL, and empty strings in CONCAT_WS
SELECT CONCAT_WS(', ', 'Seoul', 'Busan', 'Daejeon') AS city_list,
CONCAT_WS('-', 'a', NULL, 'b') AS skip_null,
CONCAT_WS('-', 'a', '', 'b') AS keep_empty,
CONCAT_WS(NULL, 'a', 'b') AS null_separator,
CONCAT('[', CONCAT_WS('-', NULL, NULL), ']') AS all_null;
The WS in CONCAT_WS means With Separator. Include both the comma and the space if you want a comma followed by a space. Below, skip_null is a-b and keep_empty is a--b. A NULL separator yields NULL. If the separator is valid and all values are NULL, the result is empty, so all_null displays []. MariaDB Docs · MariaDB CONCAT_WS()
Build an address with optional parts
Use the practice database learning and CREATE, INSERT, and SELECT permissions. If the example table already exists, choose a new practice database. These demonstration addresses include a NULL, an empty string, and three spaces in the optional district column.
USE learning;
CREATE TABLE concat_address_demo (
id INT PRIMARY KEY,
city VARCHAR(40) NOT NULL,
district VARCHAR(40) NULL,
street VARCHAR(80) NOT NULL
) DEFAULT CHARSET=utf8mb4;
INSERT INTO concat_address_demo VALUES
(1, 'Seoul', 'Jung-gu', '1 Sejong-daero'),
(2, 'Busan', NULL, '2 Jungang-daero'),
(3, 'Daejeon', '', '3 Daehak-ro'),
(4, 'Gwangju', ' ', '4 Munhwa-ro');
SELECT id,
CONCAT(city, ' ', district, ' ', street) AS plain_join,
CONCAT_WS(' ', city, district, street) AS with_separator,
CONCAT_WS(' ', city, NULLIF(TRIM(district), ''), street) AS cleaned
FROM concat_address_demo ORDER BY id;
SELECT id,
CHAR_LENGTH(CONCAT_WS(' ', city, district, street))
- CHAR_LENGTH(CONCAT_WS(' ', city, NULLIF(TRIM(district), ''), street))
AS removed_spaces
FROM concat_address_demo
WHERE id IN (3, 4)
ORDER BY id;
All three expressions agree for ID 1. For ID 2, only plain_join is NULL. The separator-based label for ID 3 has one extra space and ID 4 has four extra spaces. The second query returns removed_spaces of 1 and 4, respectively. TRIM removes leading and trailing spaces from the optional column, and NULLIF converts an empty result to NULL so the output skips that part. This changes only the displayed value. Tabs, line endings, and other whitespace may require different rules. MariaDB Docs · MariaDB TRIM() MariaDB Docs · MariaDB NULLIF()
Keep missing positions or supply fallback text
SELECT CONCAT('city=', IFNULL(NULL, '(not entered)'), '; street=', '1 Main Road')
AS explicit_missing,
CONCAT_WS('|', 'a', IFNULL(NULL, ''), 'b') AS fixed_positions,
CONCAT_WS('|', 'a', 0, 'b') AS numeric_zero;
To retain positions when a value is missing, use IFNULL with an empty string. fixed_positions returns a||b. If users need to see that a value is missing, use fallback text as in explicit_missing. numeric_zero returns a|0|b because zero is a real value. Keep the original columns if you need to recover values or their types.
If you intend to split the result again, decide how to handle separators, quotes, and line endings that appear in the data. CONCAT_WS(',', …) adds commas; producing a valid CSV requires CSV serialization.
Aggregate strings across rows with GROUP_CONCAT
SELECT GROUP_CONCAT(city ORDER BY id SEPARATOR ', ') AS cities
FROM concat_address_demo;
Use CONCAT() and CONCAT_WS() to join several values in one row. The GROUP_CONCAT example below collects cities from four rows into one list, ordered by id. Use GROUP BY for a list per group. If a list is unexpectedly short, check the input rows and group_concat_max_len. Keep display labels separate from stable identifiers. MariaDB Docs · MariaDB GROUP_CONCAT()




