I have two columns in my sql table where the phone numbers are stored. I have displayed both the numbers separated by a "," If if concatenate both the data with "," for displaying, the problem i am facing is - when the first column is blank i am getting the output as ",xxxxxxxxxx" and when the second column is blank i am getting the output as "xxxxxxxxx," I am getting the perfect output only when both the columns contain data like "xxxxxxxxx,xxxxxxxxx".
How can i remove the extra "," when either of the column data is missing !!
Please help
Thanks
Sandeep
This will do the trick.
$ret = implode(',', array_filter(array($value1, $value2)));
WHY checking that using PHP when it could be addressed inside MySQL Query??
You can do that right into the query while pulling data using CONCAT_WS.
SELECT CONCAT_WS( ',', NULL , 'col2' ) ;
OUTPUT: col2
However, this will only work if the empty field is set as NULL
. If you empty field isn't NULL, you can further put IF
condition to set that parameter as NULL
if field is empty e.g.
SELECT CONCAT_WS( ',', IF(col1 = '', NULL, col1) , 'col2' ) ;