SELECT CONCAT_WS(":", "String 1", "String 2", "String 3", "String4") AS my_str;
Output is here
String 1:String 2:String 3:String4
We used ':' as separator here and all strings are joined using this separator. We will use this in our student table. ( download sql_dump from our concat tutorial. )
SELECT concat_ws(':',f_name,' ',l_name) as name,class FROM `student_name`
| name | class |
|---|---|
| John: :Deo | Four |
| Larry: | Four |
| Ronald: | Five |
| Garry: :Miller | Five |
| Five | |
| :Ruller |
SELECT CONCAT_WS(COALESCE(f_name,'-',' '),' ',COALESCE(l_name,'-')) as name,class FROM `student_name`
| name | class |
|---|---|
| JohnDeo | Four |
| Larry- | Four |
| Ronald- | Five |
| GarryMiller | Five |
| -- | Five |
| -Ruller |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.