Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Str_Replace in SQL statement

Posted on 2005-04-01
4
Medium Priority
?
4,024 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
  • 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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

571 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