Str_Replace in SQL statement

Posted on 2005-04-01
Medium Priority
Last Modified: 2012-05-05

I have a lot of movies in a table. The titles of the movies are in the format "The Rock" without the quotes. I want to reverse the title to "Rock, The" without the quotes.

Note that I need to change the value in the field, it's not a string like in the examples at mysql.com.

Now I have this, but I do not know if this would/could work:
UPDATE movies SET title = REPLACE(title,'do-something-here','do-something-here');

Or maybe this:
UPDATE movies SET title = SUBSTRING_INDEX(title,'The ',1);
Bla bla...

Anyway,  Im stuck :-/

Thanks in advance,
Best Regards
Question by:Aknot
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 12

Expert Comment

ID: 13684626
You are on the right track.  This query should work for you:

UPDATE movies SET title=REPLACE(title, 'The Rock', 'Rock, The');


Author Comment

ID: 13685601
Thanks for your input Adrian. I need to change "all" of the titles, no matter of the titles. The thing is, how would I write the query for "_ALL_" the titles starting with "The" and then reverse "the title to "title, the"?
LVL 12

Accepted Solution

AdrianSRU earned 500 total points
ID: 13685904
UPDATE movies SET title=IF(SUBSTRING(title, 1, 4)='The ', CONCAT(SUBSTRING(title, 5), ', The'), title);

This query uses an IF statement to check if the first four characters of the title are 'The '.  If so, then it strips 'The ' off the beginning and adds ', The' to the end.


Author Comment

ID: 13686960
Thanks a lot, almost too good =)

You saved me a great deal of work =D

This statement goes directly into my collection of "Really Useful SQL Statements" =)

Thanks again, best regards

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question