MYSQL : match and remove matched word from string - mysql

I have to remove word from string when matched found
let's see
My input string is
'KENOX ROAD'
Table of words from with match perform
STREET
DRIVE
ROAD
4. DRIVE WAY
Out string should be :
KENOX
I am using vb.net for this windows form application.I am getting input string from textbox and on a button click i have to check for matches which are coming from database and remove matched words.
current solution is :
Dim strStreetName As String = txtHStreet.Text
For Each wordToRemove As DataRow In dsAbbreviation.Tables(0).Rows
If strStreetName.Contains(wordToRemove.Item("NAME")) Then
strStreetName = strStreetName.Replace(wordToRemove.Item("NAME"), "")
Exit For
End If
Next
but, I am looking for database side solution.

I think the following will work:
set #phrase = 'KENOX ROAD';
set #phrase = concat(' ', phrase, ' ')
select #phrase := replace(#phrase, concat(' ', w.word, ' '), ' '))
from words w;
set #phrase = trim(#phrase);
Your problem is a bit tricky because you seem to want the words to be identified and (unfortunately) MySQL does not have regex replace functionality. The above adds delimiters at the beginning and end and then does the replacement. Using variables like this is not a favorite approach, but I think it will work in this case.

MariaDB starts with 10.0.5 with REGEX - PCRE Regular Expressions
Mariadb REGEX Manual

MySQL does not natively support regexp replace. If you want to update an existing table you can try with something like this:
UPDATE
streets s INNER JOIN words w
ON INSTR(CONCAT(' ', s.name, ' '), CONCAT(' ', w.word, ' '))>0
SET
s.name = TRIM(
REPLACE(CONCAT(' ', s.name, ' '), CONCAT(' ', w.word, ' '), ' ')
);
and if you want to match and replace multiple words you have to re-run this update query until it doesn't find any more match. Please see a fiddle here. Not too clean, but it should do its work.
If you just want a SELECT, and you want it to support multiple words replace, I think there's no solution unless you use a programming language.

Related

How to remove \n from a mysql variable

I have the following mysql variable.
SET #theSQL = 'CREATE VIEW `Reports` AS
SELECT DISTINCT
id from mytable;'
On inspecting the variable it has the following value,
'CREATE VIEW `Reports` AS\n SELECT DISTINCT\n
Is there a way to remove the \n without having to do it manually?
Use the REPALCE function
REPLACE(#theSQL, '\n', '')
Replace will do the trick. Always replace with a space when you're handling strings containing SQL statements: removing whitespace can trash the statement. On my rig I have to cope with both \r and \n so I use this.
SET #theSQL = REPLACE(REPLACE(#theSQL , '\n', ' '), '\r', ' ');
But, Gordon is right. You can give MySQL's PREPARE a text string containing returns and newlines and it will work just fine.

Multiple string replace in apache drill using REGEXP_REPLACE sql method

I am using drill sql query on json data. But one of my json field seems to have few characters e.g. '\n' & '^' etc which I want to replace on the fly.
Currently, I am calling REGEXP_REPLACE twice as below -
SELECT REGEXP_REPLACE(REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n', ' '), '\^', ' ') FROM (VALUES(1));
How can I do that using REGEXP_REPLACE method only once?
The below must work -
SELECT REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n|\^', ' ') FROM (VALUES(1));
But note that in this case, the character you are replacing with, will be same for all. If you have to replace with different characters then you would need to go with your approach only as below -
SELECT REGEXP_REPLACE(REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n', 'X'), '\^', 'Y') FROM (VALUES(1));

How can I use this string replace in MySQL

I'm using Wordpress and have a load of custom fields where I need to do a string replace on. Basically I have a price field which is in the example format:
from 113.800
I need to remove the word 'from', the space after it and the dot between the number. All the fields are in this format, how can I use a MySQL replace function to do this?
Thanks
REPLACE() doesn't support regular expression matching, so you'll have to do it in a more clumsy way:
UPDATE wp_sometable
SET price_field = REPLACE(REPLACE(price_field, 'from ', ''), '.', '');

Replacing substrings in MySQL

I have some sentences in string in MySQL. And I need to replace substrings such as 'My' to 'my' if this word not first in sentence. How I can doing this?
CHAR, REPLACE, REPEAT, etc. I'd recommend reading mySQL ref: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html
If you just want to replace several words, you can replace them, using this approach:
UPDATE str_test SET str = REPLACE(str, ' My', ' my')
fiddle
As the words inside the text will be preceded by space. But if you want a regexp replace, it will be a more difficult task:
How to count words in MySQL / regular expression replacer?
https://dba.stackexchange.com/questions/15250/how-to-do-a-case-sensitive-search-in-where-clause
MySql support for string is very limited. A quick solution would be to use something like this:
SELECT
CONCAT(
LEFT(col, 1),
REPLACE(SUBSTRING(col, 2), 'My', 'my')
)
Please see fiddle here. This will replace all the strings My to my, except the first one.
Or maybe this:
SELECT
col,
RTRIM(REPLACE(CONCAT(col, ' '), ' My ', ' my '))
FROM
yourtable
that will replace all whole words except the first one.

How to find an exact string within a mysql column

I have a question, what is the best way to find an exact string match within a column.
I tried using locate('needle', 'haystack') > 0. The problem with this is, for instance if the string I am trying to find is something like 'Ed', but inside a blob or a text column, I have a string that says 'I lived', locate() would return 6. However, this is not what I am asking. In an exact match, it would be best to use LIKE '', however, LIKE has it's performance issues, therefore, it is not a viable solution.
Is there a way I an use LOCATE() to do an exact match?
You can use this:
WHERE CONCAT(' ', column, ' ') LIKE BINARY '% string_to_find %'
or using LOCATE:
WHERE LOCATE(BINARY ' Ed ', CONCAT(' ', column, ' '))
Using BINARY will force an exact case matching. I think that performance of LOCATE or LIKE will be very similar. Please see fiddle here.