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

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    SQL Video Tutorials










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