I have a column called SKETCH that contains vector strings varying in length, like the following:
"R24U26L24(LD26),"
Each string contains certain letters that should be followed by a number and there are parentheses() around any errors.
I want to find all the errors in each vector string - aka. any part of the string with a format like %(LD%)% or %(L%D)% [% being a wildcard] - and delete the pair of parentheses around the error, as well as any letter inside that has no number following it.
The result should look like this "R24U26L24D26,"
I've tried combining UPDATE, CASE ELSE, and REPLACE functions to run the different error criteria I have but have not succeeded. For example, the code I used below will replace the entire vector string with "%D%%" as text. Any help to figure this out is much appreciated.
UPDATE table
SET sketch = CASE
WHEN sketch LIKE '%(LD%)%' THEN REPLACE(REPLACE('%(LD%)%', '(L', ''), ')', '')
WHEN sketch LIKE '%(RU%)%' THEN REPLACE(REPLACE('%(RU%)%', '(R', ''), ')', '')
WHEN sketch LIKE '%(L%U)%' THEN REPLACE(REPLACE('%(L%U)%', '(', ''), 'U)', '')
WHEN sketch LIKE '%(L%D)%' THEN REPLACE(REPLACE('%(L%D)%', '(', ''), 'D)', '')
ELSE sketch END;
Edit: This is the final fix that I used. Not as concise as I'd hoped but it works:
UPDATE table
SET sketch = regexp_replace(sketch, '\(LD([0-9]*)\)','D\1', 'g');
UPDATE table
SET sketch = regexp_replace(sketch, '\(RU([0-9]*)\)','U\1', 'g');
UPDATE table
SET sketch = regexp_replace(sketch, '\(L([0-9]*)U\)','L\1', 'g');
UPDATE table
SET sketch = regexp_replace(sketch, '\(L([0-9]*)D\)','L\1', 'g');