MySQL function: Get string/number separated by some place holder I have the following product table

| product_id    | sku   | created_at    |
|------------   |-----  |------------   |
| 145687        | abc   | 2017-08-08    |
| 22456         | xyz   | 2017-09-01    |
| 6553214       | asd   | 2017-09-05    |


I want to get output as 1/4/5/6/8/7 with SQL

SELECT someMySQLFunction(product_id) FROM product WHERE sku = 'abc';


How to achieve this in MySQL?
For this solution I've opted for the 'easy' way out ... a user-defined function ...

Proposed function:

drop function if exists myinsert;

create function myinsert (instring char(100), inchar char(10))
returns char(100)
deterministic
begin
  declare i int;
  set i = char_length(instring);
  while i &gt; 1 do
    set instring = insert(instring,i,0,inchar);
    set i = i - 1;
  end while;
return instring;
end;



set i = char_length(instring) : get length of input string
while i/set i : loop through values of i down to '2'
insert(instring,i,0,inchar) : at position i in our string, insert inchar
at this point we've stepped backwards through instring, inserting the inschar string before every character in instring (except for the first character)


Calling the function:

select myinsert(cast(product_id as char),'/') as new_string
from   product
where  sku = 'abc';

new_string
-----------
1/4/5/6/8/7

select myinsert(cast(product_id as char),'-/-') as new_string
from   product
where  sku = 'abc';

new_string
---------------------
1-/-4-/-5-/-6-/-8-/-7


Here are rextester and a db-fiddle fiddles