?
Solved

Str_Replace in SQL statement

Posted on 2005-04-01
4
Medium Priority
?
4,015 Views
Last Modified: 2012-05-05
Hi!

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
0
Comment
Question by:Aknot
[X]
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
4 Comments
 
LVL 12

Expert Comment

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

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


--Adrian
0
 

Author Comment

by:Aknot
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"?
0
 
LVL 12

Accepted Solution

by:
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.


--Adrian
0
 

Author Comment

by:Aknot
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
0

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