By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE `test` (
`id` INT,
`countryCode` VARCHAR(50),
`state` VARCHAR(50)
) ENGINE=INNODB DEFAULT CHARSET=UTF8MB4 COLLATE = UTF8MB4_0900_AI_CI;
insert into `test` values
(1,'AE','AE'),
(2,'SA','SA'),
(3,NULL,'SA') ;
SELECT 'id|countryCode|state|' AS `line`
UNION ALL
SELECT CONCAT_WS('|', `id`, COALESCE(`countryCode`, ''), COALESCE(`state`, '')) AS `line`
FROM `test`;
line |
---|
id|countryCode|state| |
1|AE|AE |
2|SA|SA |
3||SA |