Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Modify Query from MS SQL to MYSQL

Posted on 2011-03-10
17
Medium Priority
?
256 Views
Last Modified: 2012-05-11
I want to convert this update to work on mysql:
update posts set post = '[media]' + SUBSTRING(post, charindex('=', post) + 1, charindex(']', post, 2) - charindex('=', post) - 1) + '[/media]'

Open in new window

0
Comment
Question by:Nathan Riley
[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
  • 9
  • 8
17 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35102320
try this.
UPDATE posts 
   SET post = '[media]' + SUBSTRING(post,LOCATE('=',post) + 1,LOCATE(']',post,2) - LOCATE('=',post) - 1) + '[/media]'

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35102365
that returned all 0's
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35102374
Can you post some sample value for post column before and after update on MSSQL?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:Nathan Riley
ID: 35102376
before

[url=http://domain.com/blah.flv]blah video[/url]
[url=http://domain.com/video/name.mp4]name of him[/url]
[url=http://domain.com/video/2001/funny.flv]funny clip[/url]

Open in new window


after
[media]http://domain.com/blah.flv[/media]
[media]http://domain.com/video/name.mp4[/media]
[media]http://domain.com/video/2001/funny.flv[/media]

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35103117
Try this query.
UPDATE Posts 
   SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post)), 
                     '[/media]');

Open in new window


Tested on your sample data.
SELECT * FROM Posts;
+--------------------------------------------------------------+
| post                                                         |
+--------------------------------------------------------------+
| [url=http://domain.com/blah.flv]blah video[/url]             |
| [url=http://domain.com/video/name.mp4]name of him[/url]      |
| [url=http://domain.com/video/2001/funny.flv]funny clip[/url] |
+--------------------------------------------------------------+
3 rows in set (0.00 sec)

UPDATE Posts 
   SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post)), 
                     '[/media]');
 
SELECT * FROM Posts; 
+-------------------------------------------------------------+
| post                                                        |
+-------------------------------------------------------------+
| [media]http://domain.com/blah.flv]blah [/media]             |
| [media]http://domain.com/video/name.mp4]name [/media]       |
| [media]http://domain.com/video/2001/funny.flv]funny[/media] |
+-------------------------------------------------------------+
3 rows in set (0.00 sec)

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35103127
that's close, but the result set is wrong

you have:
[media]http://domain.com/blah.flv]blah [/media]             
[media]http://domain.com/video/name.mp4]name [/media]       
[media]http://domain.com/video/2001/funny.flv]funny[/media]

Open in new window


I need:
[media]http://domain.com/blah.flv[/media]
[media]http://domain.com/video/name.mp4[/media]
[media]http://domain.com/video/2001/funny.flv[/media]

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35103317
check this.
UPDATE Posts 
   SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post) - LOCATE('http',post)),
                     '[/media]');

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35107970
returns
[media][/media]

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35113217
Because you already ran my first UPDATE statement. Run my last update statement only.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35113220
If possible, truncate your table and re-insert the records, then run my last update statement.
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35113237
I did that, same result when I do select * from posts I see the post field has data in it.  When I run your query it only shows
[media][/media]

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35113291
I didn't run the update I ran the select b/c I don't want to update until I get the results I want.

select CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post) - LOCATE('http',post)),
                     '[/media]') from posts;

Open in new window



result was
[media][/media]

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35113300
I ran the UPDATE statement and got the correct result.
mysql> truncate table Posts;
Query OK, 3 rows affected (0.06 sec)

mysql> insert into Posts values ('[url=http://domain.com/blah.flv]blah video[/url]');
Query OK, 1 row affected (0.00 sec)

mysql> insert into Posts values ('[url=http://domain.com/video/name.mp4]name ofhim[/url]');
Query OK, 1 row affected (0.00 sec)

mysql> insert into Posts values ('[url=http://domain.com/video/2001/funny.flv]funny clip[/url]');
Query OK, 1 row affected (0.00 sec)

mysql> select * from Posts;
+--------------------------------------------------------------+
| post                                                         |
+--------------------------------------------------------------+
| [url=http://domain.com/blah.flv]blah video[/url]             |
| [url=http://domain.com/video/name.mp4]name of him[/url]      |
| [url=http://domain.com/video/2001/funny.flv]funny clip[/url] |
+--------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> Update Posts SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post)-LOCATE('http',post)) ,'[/media]');
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from Posts;
+-------------------------------------------------------+
| post                                                  |
+-------------------------------------------------------+
| [media]http://domain.com/blah.flv[/media]             |
| [media]http://domain.com/video/name.mp4[/media]       |
| [media]http://domain.com/video/2001/funny.flv[/media] |
+-------------------------------------------------------+
3 rows in set (0.00 sec)

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35113319
ah I see the problem I think.  I need to do a from after to make sure I only get certain records, but not working:

UPDATE Posts 
   SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post) - LOCATE('http',post)),
                     '[/media]')
from posts
where post like '%[url=%'
and post like '%[/url]%';

Open in new window

0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 35113332
created another database and restored the backup there and did your original update.  It updated all the rows to
[media][/media]

Open in new window

0
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 35113333
try this.
UPDATE Posts 
   SET Post = CONCAT('[media]',SUBSTRING(post,LOCATE('http',post),LOCATE(']',post)-LOCATE('http',post)) ,'[/media]') 
 WHERE Post LIKE '[url=%';

Open in new window

0
 
LVL 12

Author Closing Comment

by:Nathan Riley
ID: 35113977
That worked thanks for sticking with me.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 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