nmarano
asked on
Find and replace part of a string
Experts-
I have string responses stored in my table. Some of the responses contain "dbmSnippets" within the response string. I would like to find all of the dbmSnippets and replace them with \' keeping the rest of the response in tact. Any suggestion on how to do this would be appreciated.
Thanks
Nick
I have string responses stored in my table. Some of the responses contain "dbmSnippets" within the response string. I would like to find all of the dbmSnippets and replace them with \' keeping the rest of the response in tact. Any suggestion on how to do this would be appreciated.
Thanks
Nick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It looks like the current query is replacing it with a slash '\' -- update the code to be REPLACE(response, 'dbmSnippets', '\'') and it will result in only the dbmSnippets being replaced. The rest of the string will be untouched.
No, it will not. I have thought you want \, not '.
You need:
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', "'") WHERE response LIKE '%dbmSnippets%';
WHERE response LIKE '%dbmSnippets%' will look for all rows for which a column response contains dbmSnippets.
REPLACE(response, 'dbmSnippets', "'") will produce a string that will be a content of response with all dbmSnippets replaced with '.
Before you run it over your production table, create a test table and try the query on it.
You need:
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', "'") WHERE response LIKE '%dbmSnippets%';
WHERE response LIKE '%dbmSnippets%' will look for all rows for which a column response contains dbmSnippets.
REPLACE(response, 'dbmSnippets', "'") will produce a string that will be a content of response with all dbmSnippets replaced with '.
Before you run it over your production table, create a test table and try the query on it.
ASKER
Sorry net correct, I want /'
Just put what you want as the third parameter for REPLACE.
ASKER
Net, I'm looking for a slash and an apostrophe so the replace will be /'
Thanks
nick
Thanks
nick
You are not limited to a single character. Try:
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', "/'") WHERE response LIKE '%dbmSnippets%';
or
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', '/\'') WHERE response LIKE '%dbmSnippets%';
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', "/'") WHERE response LIKE '%dbmSnippets%';
or
UPDATE tablename SET response = REPLACE(response, 'dbmSnippets', '/\'') WHERE response LIKE '%dbmSnippets%';
ASKER
Hey Net,
Sorry for the confusion. So, I'm looking to use \' and not the back slash/. Now I tested it on a couple of results, but it's taking the dbmSnippets out but only replacing it with a ' instead of \'....
Sorry for the confusion. So, I'm looking to use \' and not the back slash/. Now I tested it on a couple of results, but it's taking the dbmSnippets out but only replacing it with a ' instead of \'....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please credit, Net, by the way. Just helping you get the understanding you need.
ASKER
thank you guys for your help. Truly appreciated. Net, thanks for the query, and mwvisa, thanks for the links.
ASKER
Will that keep the rest of my string intact? So if my string looks like,
Yes~I agree that JuliadbmSnippetss quadlateral performance" Will it simply replace the dbmSnippets to look like...
"Yes~I agree that Julia's quadlateral performance"