There is no regex replace capability built in to MySQL.
You can, however, accomplish your purpose by doing what you suggested -- counting the number of ^
and crafting a string of replacement values, with this:
TRIM(TRAILING '^' FROM REPEAT('0.00^',(LENGTH(column) - LENGTH(REPLACE(column,'^','')) + 1)));
From inside to outside, we calculate the number of values by counting the number of delimiters, and adding 1 to that count. We count the delimiters by comparing the length of the original string, against the length of the same string with the delimiters stripped out using REPLACE(...,'^','')
to replace every ^
with nothing.
The REPEAT()
function builds a string by repeating a string expression n number of times.
This results in a spurious ^
at the end of the string, which we remove easily enough with TRIM(TRAILING '^' FROM ...)
.
SELECT t1.*,
... the expression above ... FROM table_name t1
, from your table to verify the results of this logic (replacing column
with the actual name of the column), then you can UPDATE table SET column = ...
to modify the values. once you are confident in the logic.
Note, of course, that this is indicative of a problematic database design. Each column should contain a single atomic value, not a "list" of values, as this question seems to suggest.