Solved

Modify Query from MS SQL to MYSQL

Posted on 2011-03-10
17
252 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
[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 11

Author Comment

by:N R
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

Author Comment

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

733 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