Replace value within a comma-delimited string in MySQL? - mysql

Suppose I have the following comma-delimited column value in MySQL: foo,bar,baz,bar,foo2
What is the best way to replace whatever is in the 4th position (in this case bar) of this string with barAAA (so that we change foo,bar,baz,bar,foo2 to foo,bar,baz,barAAA,foo2)? Note that bar occurs both in position 2 as well as position 4.
I know that I can use SUBSTRING_INDEX() in MySQL to get the value of whatever is in position 4, but have not been able to figure out how to replace the value in position 4 with a new value.
I need to do this without creating a UDF or stored function, via using only the standard string functions in MySQL (http://dev.mysql.com/doc/refman/5.5/en/string-functions.html).

Hmm... maybe this?
SELECT #before := CONCAT(SUBSTRING_INDEX(`columnname`,',',3),','),
#len := LENGTH(SUBSTRING_INDEX(`columnname`,',',4)+1
FROM `tablename` WHERE ...;
SELECT CONCAT(#before,'newstring',SUBSTRING(`columnname`,#len+1)) AS `result`
FROM `tablename` WHERE ...;
Replace things as needed, but that should just about do it.
EDIT: Merged into one query:
SELECT
CONCAT(
SUBSTRING_INDEX(`columnname`,',',3),
',newstring,',
SUBSTRING(`columnname`, LENGTH(SUBSTRING_INDEX(`columnname`,',',4)+1))
) as `result`
FROM `tablename` WHERE ...;
That +1 may need to be +2, I'm not sure, but that should work.

You first split your problem in two parts:
locate the comma and split the string in values separated by comma.
update the table with same string and some substring appended.
For the first part I would suggest you take a look here
And for the second part you should take a look here
One more thing there is no shortcut to any problem. You should not run from the problem. Take it as a challenge. Learn while you search for the answer. Best thing take guidance from here and Try to do more researching and efforts.

Try this:
UPDATE yourtable
SET
categories =
TRIM(BOTH ',' FROM
REPLACE(
REPLACE(CONCAT(',',REPLACE(col, ',', ',,'), ','),',2,', ''), ',,', ',')
)
WHERE
FIND_IN_SET('2', categories)
taken from here The best way to remove value from SET field?

Related

MySQL substring between two strings

I need a hand to solve a problem with my column field.
I need to extract the string in between these two different "patterns" of strings for example:
[...string] contract= 1234567890123350566076070666 issued= [string
...]
I want to extract the string in between 'contract=' and 'issued='
At the present moment I'm using
SELECT substring(substring_index(licence_key,'contract=',-1),1,40) FROM table
The problem is that this string in between doesn't have always 40 characters so it's not fixed length and so the data that comes before and after that. It's a volatile data.
Do you known how I can handle that?
Just use substring_index() twice:
SELECT substring_index(substring_index(licence_key, 'contract=', -1),
'issued=', 1)
FROM table;
If this string does not match then give the total result.
If you want to replace then you can use like this.
UPDATE questions set question= REPLACE(question, '<xml></xml>', '') WHERE question like '%<xml>%';
UPDATE questions set question= REPLACE(question, substring_index(substring_index(question, '<xml>', -1), '</xml>', 1), '') WHERE question like '%<xml>%';

MySql : query to format a specific column in database table

I have one column name phone_number in the database table.Right now the numbers stored in the table are format like ex.+91-852-9689568.I want to format it and just want only digits.
How can i do it in MySql ? I have tried it with using functions like REGEXP but it displays error like function does not exist.And i don't want to use multiple REPLACE.
One of the options is to use mySql substring. (As long as the format doesn't change)
SELECT concat(SUBSTRING(pNo,2,2), SUBSTRING(pNo,5,3), SUBSTRING(pNo,9,7));
if you want to format via projection only, use SELECT, you will only need to use replace twice and no problem with that.
SELECT REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
FROM tableName
otherwise, if you want to update the value permanently, use UPDATE
UPDATE tableName
SET columnName = REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
MySQL does not have a builtin function for pattern-matching and replace.
You'll be better off fetching the whole string back to your application, and then using a more flexible string-manipulation function on it. For instance, preg_replace() in PHP.
Try the following and comment please.
Select dbo.Regex('\d+',pNo);
Select dbo.Regex('[0-9]+',pNo);
Reference on RUBLAR.
So MYSQL is not like Oracle, hence you may just use a USer defined Function to get numbers. This could get you going.

How to remove -XXXX from Zip Code field using MySQL REGEXP?

Can someone give me an example of how to remove anything after a dash "-" in a zip code field using SQL commands?
For example, change any of this:
XXXXX-X
XXXXX-
XXXXX-XX
XXXXX-XXXX
to this:
XXXXX
Thanks for the examples. I also need to remove any instances of "-", "-X', "-XXXX", etc in the databases so the zip codes just contain five digits. Can someone include an example of this?
Given that your field is named zip , then just do this:
SELECT SUBSTRING_INDEX(zip, '-', 1) as zip
that would return what you want.
To update the data on the table you can do:
update table set zip = SUBSTRING_INDEX(zip, '-', 1) where condition = foo
that would update only records matching some condition, if you want to update them all remove the where part
Instead of using a regex, you could use MySQL's SUBSTRING_INDEX() method:
SELECT SUBSTRING_INDEX(zip_code, '-', 1) FROM your_table
EDIT (to support updates)
UPDATE your_table SET zip_code = SUBSTRING_INDEX(zip_code, '-', 1);
I'd recommend creating a second column, maybe zip_code_short and running SET zip_code_short = instead of overwriting the main data - just to make sure it doesn't cause any errors first (if feasible).

Replace an exact string in mysql that includes square brackets

I am very new to sql querys and I am looking for a way to remove the following string from anywhere in my database: [lesson-navigation] including the square brackets. (Some instances have an unnecessary extra space before and/or after the string that would be nice to delete as well if possible too.
This was a shortcode that I was inserting into my site, but then decided to implement it into the template instead. Now I have over 2000 instances of this in various places in my database and want to remove it.
Thanks in advance for your help.
~Cam
Use REPLACE() to perform a string replacement that removes the string from each column. If this string appears in multiple columns, you'll need to perform the query below for each column, or specify each column in the query (second example):
UPDATE tablename SET columnname = REPLACE(columnname, '[lesson-navigation]', '');
/* Replace in multiple columns */
UPDATE tablename SET
columnname = REPLACE(columnname, '[lesson-navigation]', ''),
columnname2 = REPLACE(columnname2, '[lesson-navigation]', ''),
columnname3 = REPLACE(columnname3, '[lesson-navigation]', '')
;

MySQL search to ignore hyphens

How can I search for "1-800-flowers" by "1800flowers" in MySQL?
I have the data "1-800-flowers", but I want to find it by "1800flowers".
You're probably best off creating a second column that you fill with 1800flowers (replacing all characters you want to ignore) and searching that. That way, you can make full use of indexing.
A quick way to convert all existing data would be
UPDATE table SET columnname_without_hyphens = REPLACE(columnname, "-", "");
If your problem is just ignoring hyphens, I may suggest using REPLACE to eliminate them, like this:
SELECT ... WHERE REPLACE(column, '-', '') ...
Otherwise, if you're looking for strings that "sound alike", you may want to have a look at the SOUNDEX function.
The use of the replace function will kill any ability to use an index on the column, but:
select *
from YourTable
where replace(YourColumn, '-', '') = '1800flowers'