0

I have a lot of rows in database where column "info" is ie. "Size, inch: 12x13<br>Material: paper<br>Amount: 100pcs". Now I need to find/select all rows that has this string part "Size, inch:" and replace/fix those rows' colums to ie. "Size: 12x13 inch<br> Material: paper<br>Amount: 100pcs ".

How in the world I write a sql statement for this? Do I need some magic regexp for sql? How do I replace and modify parts of a string in multiple rows?

EDIT: The numbers can be anything (ie. 12x13 or 44x55 or 77x88x99 etc.) So there would have to be some kind of wildcard for the numbers, perhaps?

I need to change "Size, inch: 'anynumbershere' 'anythingafter the numbers'" to "Size: 'anynumbershere' inch 'anythingafter'".

1
  • Check for mysql replace function. Commented Jul 22, 2015 at 10:29

2 Answers 2

1
update mytable set info=REPLACE ( info , 'Size, inch: 12x13 ', 'Size: 12x13 inch' )
where info like '%inch:12X13%'
Sign up to request clarification or add additional context in comments.

1 Comment

My numbers 12x13 are different in every row. I'm not sure this works. In some rows there can be ie. "Size, inch: 45x88x99<br> and so on..."
0

Perhaps this will work:

update table t
    info = replace(replace(info, 'Size inch:', 'Size:'), '<br>Material', ' inch<br>Material')
    where info like 'Size inch:%<br>Material%';

Note: this assumes that 'Size inch' and '<br>Material' only appear once in each string.

3 Comments

The "material" after <br> isn't always there. This is my point why this is painful. I need to change "Size, inch: <anynumbershere><anythingafter the numbers>" to "Size: <anynumbershere> inch<anythingafter>".
@H3ll1n . . . We can only answer the question that is asked. If you have a different question, you should ask it as a different question.
My bad. I got too specific.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.