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



plus2net.com



Post your comments , suggestion , error , requirements etc here .










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