MySQL : replace occurence of a string in field except first one - mysql

I want to update all fields of a column, a lot of them have a desired string in there, but I want this string to be in only each field once, for instance :
"MyString OtherString MyString AnotherString AndAnother MyString"
to
"MyString OtherString AnotherString AndAnother"
would you have any idea on how to achieve this ?

If "MyString" will always occur as the first term in the field, this would work:
update MyTable set MyField = replace(MyField, ' MyString','')
The key point above is that we look for occurrences of "MyString" with a leading space, so the first occurrence at the beginning of the field will be ignored.
However, my guess is this might be too fragile - what if the first occurrence of "MyString" is not at the beginning of the field?
in this latter case you need the following:
UPDATE
MyTable
SET
MyField =
CONCAT(
LEFT(MyField,INSTR(MyField,'MyString') + LENGTH('MyString')),
REPLACE(RIGHT(MyField, LENGTH(MyField) - (INSTR(MyField,'MyString') + LENGTH('MyString'))), 'MyString','')
)
What this does is to split the field into two, the first part up to and including the first occurrence of "MyString", and the second part replacing all further occurrences of it.

Related

How to replace two sub-strings in one field and insert the result in another field in MySQL

I have a field oldCol that contains string separated by , (comma). I just discovered that this is inconvenient as the ,can exist in URLs. So I want to replace it with a character that can never appear in a URL like >:
https://11.com/,http://2,2.com/
https://aa.com/,https://www.bb,b.com/,http://www.abcc.org/homePage
The separator comma that I want to replace is placed before http or https. I do not want to change the original column. So I created a new one newCol and I want to set its value to the modified column. So I wrote:
UPDATE tbl
SET newCol = replace(oldCol, ',https://', '>https://');
But the problem is that the above statement only captures https:// and I need to also replace those with http:// in the same time so the value of newCol consists of oldCol with separator commas converted to >. That is, the newCol in the above example is:
https://11.com/>http://2,2.com/
https://aa.com/>https://www.bb,b.com/>http://www.abcc.org/homePage
You can replace twice:
UPDATE tbl
SET newCol = replace(replace(oldCol, ',https://', '>https://'), ',http://', '>http://');

Mysql : removing all occurences of a string in a field except first causing odd behaviour

I was looking for a way to let only one occurence of the same string in a field a fews days ago, and someone provided me with a really nice answer.
The idea was to turn a field like that :
MyString OtherString MyString AnotherString AndAnother MyString
into
MyString OtherString AnotherString AndAnother
The solution I was given is this :
UPDATE
MyTable
SET
MyField =
CONCAT(
LEFT(MyField,INSTR(MyField,'MyString') + LENGTH('MyString')),
REPLACE(RIGHT(MyField, LENGTH(MyField) - (INSTR(MyField,'MyString') + LENGTH('MyString'))), 'MyString','')
)
While it does the job, it messes up in some fields, turning the 'MyString' into
'MyyyyyStrrrriing' or 'MyString ng ng', 'MyString ing' and so on(there's so many variations going on)
heck, even on another string in the field, such as :
OtheeeeeeeeeeeeeeeeeeeeeeerString
AndAnothertherthertherther
Any idea on how to perfect this solution even further ?
original question :
MySQL : replace occurence of a string in field except first one

tsql last "occurrence of" inside a string

I have got field containing comma separated values. I need to extract the last element in the list.
I have tried with this:
select list_field, LTRIM(RTRIM(right(list_field, len(list_field) - CHARINDEX(',',list_field))))
But it returns the last part of the list just starting after the first comma occurrence.
For example,
a,b returns b
a,b,c returns b,c
I would like to use a regex like pattern. Is it possible in TSQL (sql server 2008)?
Any other clues?
Find the last , by reversing the string and looking for the first occurrence, then read that many characters from the right of the string;
rtrim(right(list_field, charindex(',', reverse(list_field)) - 1))
(Use reverse(list_field) + ',' if there is the possibility of no delimiters in the field & you want the single value)

Replace in mysql database

I have a question about replacement a particular string in mysql but one part of the string is is changed every time e.g
"(my string to replace 1224:2)"
"(my string to replace 134:4)"
"(my string to replace 1824:9)"
"(my string to replace 14:2)"
I can change first part of string using this query
update dle_post set short_story = replace(short_story,'(my','( my');
but how to replace other parts like 1224:2) , or 14:2) or any other part that ends with a number 1,2,3.. and a ). I can not use bracket ")" because it is used on many other places.
Not the most elegant way, but...
update dle_post
set short_story =
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
short_story,
'0)','0 )'),'1)','1 )'),'2)','2 )'),'3)','3 )'),'4)','4 )'),'5)','5 )'),'6)','6 )'),'7)','7 )'),'8)','8 )'),'9)','9 )');
Regular expressions should work in this kind of case. Take a look related question: How to do a regular expression replace in MySQL?

MySQL sort by name

Is ir possible to sort a column alphabetically but ignoring certain words like e.g 'The'
e.g.
A normal query would return
string 1
string 3
string 4
the string 2
I would like to return
string 1
the string 2
string 3
string 4
Is this possible?
EDIT
Please note I am looking to replace multiple words like The, A, etc... Can this be done?
You can try
SELECT id, text FROM table ORDER BY TRIM(REPLACE(LOWER(text), 'the ', ''))
but note that it will be very slow for large datasets as it has to recompute the new string for every row.
IMO you're better off with a separate column with an index on it.
For multiple stopwords just keep nesting REPLACE calls. :)
This will replace all leading "The " as an example
SELECT *
FROM YourTable
ORDER BY REPLACE(Val,'The ', '')
Yes, it should be possible to use expressions with the ORDER-part:
SELECT * FROM yourTable ORDER BY REPLACE(yourField, "the ", "")
I have a music listing that is well over 75,000 records and I had encountered a similar situation. I wrote a PHP script that checked for all string that began with 'A ', 'An ' or 'The ' and truncated that part off the string. I also converted all uppercase letters to lowercase and stored that string in a new column. After setting an index on that column, I was done.
Obviously you display the initial column but sort by the newly-created indexed column. I get results in a second or so now.