skip to Main Content

In my database I have 1000+ rows of data that I need to modify a bit. The content column. I’ve given it try and the closest i’ve come was using RPAD

UPDATE TABLE_NAME SET content = RPAD("mySlides/", LENGTH(username)+9, username);

Using RPAD remove all the text after though, so I believe RPAD isn’t the way to go about this.

I’m trying to insert the username column after the "mySlides/" text but also not to remove the following text after. Just place right in between.

Is this possible?

Here is an example I mocked up.
enter image description here

Any help is appreciated!

2

Answers


  1. UPDATE table_name 
    SET content = REPLACE( content, 
                           'mySlides/', 
                           CONCAT( 'mySlides/', 
                                   username, 
                                   '/'
                                  )
                           )
    
    Login or Signup to reply.
  2. One way to achieve this is by using the CONCAT function.

    Here an exemple :

    UPDATE TABLE_NAME_C
    SET content = CONCAT('<img src="../myslides/', username, '/', SUBSTRING(content, INSTR(content, '/')+1));
    

    Explanation

    INSTR() is used to finds the position of the first occurrence of ‘/’. SUBSTRING() extracts the substring of the content column starting from the position after the first occurrence of the character ‘/’ till the end of the string.

    db fiddle: https://www.db-fiddle.com/f/fpoiGLnEzFQAmFRkkibEWn/1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search