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.
Related
Given the value of name column: AL TAIRAWI TRADING ESTABLISHMENT
Update ae_companies set uniqueidentifier = CONCAT(trim(LEFT(name, 5)), '_', id, '_ae')
Above query produces this:
AL TA_6_ae
What I am looking for is this:
ALTA_6_ae
I have no idea why the trim() isn't working here.
Use replace() instead of trim().
Update ae_companies
set uniqueidentifier = CONCAT(replace(LEFT(name, 5), ' ', ''), '_', id, '_ae')
TRIM() isn't working (doing the job you thought it does) in your case because its description is:
Remove leading and trailing spaces
When you do LEFT(name, 5) which produces AL TA all leading and trailing spaces are removed when you wrap this around TRIM() the way you did. In your case space character is neither at the last nor at the first position, so this is why your result doesn't change when trimmed.
What you're looking for is a REPLACE($INPUT_STRING, ' ', '') to truncate all occurences of space character within the input string.
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.
Take a look : FIDDLE
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))=TRIM(replace('IKECHUKWU OSUJI','
',''))),"same","diff");
select IF((TRIM(replace(' Aman Minhas ',' ',''))=TRIM(replace(' Aman Min has','
',''))),"same","diff");
The first query returns diff. The second returns same. Its some weird spacing issue, cant seem to understand why this behaviour.
Your first string has a tab in it:
select IF((TRIM(replace(' IKECHUKWU OSUJI',' ',''))
^ this is actually a tab in the Fiddle
You can get rid of it with an additional REPLACE:
REPLACE(REPLACE(myString, ' ', ''), '\t', '')
The \t is a special literal. Other special literals such as newline or ASCII NUL may impact you as well. Literals are listed here.
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 ', ''), '.', '');
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.