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?
Related
I have a read only database. where i want to compare two string. But in the database some column value name has included some special character but i want to compare another string with this string without special character.
For example, In DB the column value name is BMW 3-SERIES. And i have another string BMW3SERIES. So, i want to remove - and space then compare two string in a where condition. Is both string equal or not ?
I am stuck how to remove special characters from model_name .
SELECT * FROM VENDOR WHERE model_name = "BMW3SERIES"
What should be the query?
On MySQL 8+, you could use REGEXP_REPLACE to remove what you deem as special characters, for example:
SELECT *
FROM VENDOR
WHERE REGEXP_REPLACE(model_name, '[ -]+', '') = 'BMW3SERIES';
You may add whatever characters you wish to the above character class [ -]+.
Problem
I'm trying to alter a variable to my desired string which includes a set parentheses. When doing so, it's trims off the closing parenthesis. I believe I have to include string literals but unsure on how to do so for my particular scenario.
Script (Partial)
...
IF col_datatype = 'varchar' THEN
SET col_datatype = 'varchar(30)';
SELECT col_datatype;
END IF;
SELECT col_datatype;
...
Output
Try using the backquote `. This is used when you wish to insert non-formatted strings into the database.
I have a table named testlink and it has url and newtarget columns.
I would like to take the string expressions https://domain1.com/ here in the url column and change all the data in the newtarget column to https://domain1.com/search/?q= pulled string expression.
So briefly;
url columns from https://domain1.com/topic1
will be changed to https://domain1.com/search/?q=topic1 in the newtarget column
There are about 6 thousand different topics (lines) available.
Database: Mysql / Phpmyadmin.
use REPLACE
UPDATE testlink
SET newtarget = REPLACE(url,'https://domain1.com/','https://domain1.com/search/?q=')
MySQL REPLACE() replaces all the occurrences of a substring within a
string.
REPLACE(str, find_string, replace_with)
If you want to conditionally change the value, you can use string manipulations:
update t
set url = concat(left(url, length(url) - length(substring_index(url, '/', -1))), 'q=', substring_index(url, '/', -1))
where url like 'https://domain1.com/%';
This uses substring_index() to get the last part of the string (after the last /). It uses left() to get the first part (based on the length of the last part) and then concatenates the values you want.
Of course, test this logic using a SELECT before implementing an UPDATE.
If you're using MySQL 8, then you'd be able to do that with REGEXP_REPLACE.
For your example, this should work :
SELECT REGEXP_REPLACE('https://domain1.com/topic1','(https:\/\/domain1\.com\/)(.+)','$1search/?q=$2')
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.
I'm trying to find a way to run a query which selects where the first two characters of an eight character string are Not numeric.
I've found a way to select if the entire string is numeric:
SELECT str FROM tbl
WHERE str REGEXP('(^[0-9]+$)');
So from my limited knowledge of regex I'm guessing that I'll need to use something like:
SELECT str FROM tbl
WHERE str REGEXP('(^[A-Z]+$)');
(I'm OK to use capitals for this as thats how the codes are stored)
I just don't know how to apply this test to just the first 2 characters of the string instead of the entire string?
^[A-Z]{2}
Try this.This should do it.