Solved

Modify Query from MS SQL to MYSQL

Posted on 2011-03-10
17
250 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

786 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