Solved

Modify Query from MS SQL to MYSQL

Posted on 2011-03-10
17
248 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:N R
  • 9
  • 8
17 Comments
 
LVL 40

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 11

Author Comment

by:N R
ID: 35102365
that returned all 0's
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35102374
Can you post some sample value for post column before and after update on MSSQL?
0
 
LVL 11

Author Comment

by:N R
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 40

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 11

Author Comment

by:N R
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 40

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 11

Author Comment

by:N R
ID: 35107970
returns
[media][/media]

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

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

Expert Comment

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

Author Comment

by:N R
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 11

Author Comment

by:N R
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 40

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 11

Author Comment

by:N R
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 11

Author Comment

by:N R
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 40

Accepted Solution

by:
Sharath earned 500 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 11

Author Closing Comment

by:N R
ID: 35113977
That worked thanks for sticking with me.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now