Query for converting a column of numbers matching a regex in MySQL
0
votes
1
answer
210
views
I have a column which contains string values of the format e.g
AB12345678
- two letters followed by 8 numeric digits.
I need to write a MySQL query to insert a 5
before each set of 4 digits thus AB12345678
becomes AB5123455678
.
I'm close, the following works for one value in the column, but not for multiple values - all values are written with the same first value encountered it seems.
SELECT @twoeight := os_grid_ref FROM projects WHERE os_grid_ref REGEXP '[A-Z]{2}[0-9]{4}[0-9]{4}';
SELECT @prefix1 := SUBSTR(@twoeight,1,2);
SELECT @part1 := SUBSTR(@twoeight,3,4);
SELECT @part2 := SUBSTR(@twoeight,7,4);
UPDATE projects SET os_grid_ref=CONCAT(@prefix1,'5',@part1,'5',@part2) WHERE os_grid_ref REGEXP '[A-Z]{2}[0-9]{4}[0-9]{4}';
What do I need to adjust so that the query can modify each value in the column individually.
Thank you.
Asked by therobyouknow
(133 rep)
Nov 21, 2023, 09:34 PM
Last activity: Nov 21, 2023, 09:58 PM
Last activity: Nov 21, 2023, 09:58 PM