Home > Mobile >  Add comma between every character of a string on mysql
Add comma between every character of a string on mysql

Time:02-02

Is it possible to add a comma between each character of a string on mysql.

From my_table

id name
1 hello

To :

id name
1 h,e,l,l,o

CodePudding user response:

If you are using MySQL 8.0, you could try REGEXP_REPLACE function.

SELECT REGEXP_REPLACE(name, "(.)(?!$)", "$1,")
FROM my_table

See demo here

CodePudding user response:

Another MySQL8 REGEXP_REPLACE -

SELECT id, REGEXP_REPLACE(name, '(?=.)(?<=.)', ',')
FROM my_table;

db<>fiddle and regular expressions 101

Since MySQL 8.0.4 the regex implementation has been using International Components for Unicode (ICU) - patterns and behavior are based on Perl’s regular expressions

CodePudding user response:

try this one

$outString = implode(
    ',',
    str_split($string)
);
echo $outString ;
  • Related