CONCAT_WS: Concatenate With Separator

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`
nameclass
John: :DeoFour
Larry: Four
Ronald: Five
Garry: :MillerFive
Five
:Ruller
You can see in above records if any one of the string ( or column ) is null then the value returned by CONCAT_WS is null. We can change this and return any specific string by using COALESCE.
SELECT CONCAT_WS(COALESCE(f_name,'-',' '),' ',COALESCE(l_name,'-')) as name,class  FROM `student_name`
nameclass
JohnDeoFour
Larry-Four
Ronald-Five
GarryMillerFive
--Five
-Ruller

SQL String References substring_index to get part of string using delimiter

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer