Change partial string on a field - mysql

I have a field "my_data" with strings like these :
XX-12-XXXX
12-XX-2000
XX-XX-2000
XX-XX-XXXX
and I'd like to change every XX and XXXX with, respectively, 00 and 0000
How can I do it with a MySql query?

Use the REPLACE function:
UPDATE myTable
SET my_data = REPLACE(my_date, 'XX', '00')
Note:
I am using XX as this will also replace XXXX, but not single occurrences of X.

You cam use REPLACE() function:
UPDATE table
SET my_data = REPLACE(my_data, 'X', '0');
SELECT REPLACE(my_data, 'X', '0') AS myData
FROM table;

Related

MySQL query for string replacement and set String

I have string data in one cell and column type text.
like example :
Q: {"newColumnName":"xxxx","filterList":[],"columnNames":"pqrs"}
columnNames,filterList,newColumnName are not in fixed position may be change.
i need to replace this above string data to
Need Result:{"columnNames":["pqrs"],"filterList":[],"parameters":{"newColumnName":"xxxx"}}
means i need to search and replace :-
1. "columnNames":"pqrs"------>"columnNames":["pqrs"].
2. "newColumnName":"xxxx" ------>"parameters":{"newColumnName":"xxxx"}.
pqrs and xxxx known to me. It can any string in there.
you can do it with a query like this:
It is ONLY a text replacement
to check
SELECT t.val
, REPLACE(
REPLACE(t.val,'"columnNames":"pqrs"','"columnNames":["pqrs"]')
, '"newColumnName":"xxxx"' , '"parameters":{"newColumnName":"xxxx"}') as result
FROM tt t;
to replace
UPDATE tt
set tt.val =
REPLACE(
REPLACE(tt.val,'"columnNames":"pqrs"','"columnNames":["pqrs"]')
, '"newColumnName":"xxxx"' , '"parameters":{"newColumnName":"xxxx"}');

Replace substring in a encrypted field in msql

I have an encrypted column in mysql . I need to replace a substring in it .
If it was not encrypted then I would have used
UPDATE my_table
SET my_field = REPLACE(my_field, 'olddata', 'newdata')
If it was entire column updation , I would use
UPDATE my_table
SET my_field = AES_ENCRYPT('newdata' , 'KEY')
where AES_DECRYPT(my_field , 'KEY') = 'olddata'
But how do I use both the above codes together ? REPLACE with AES_ENCRYPT ?
You'd need to:
decrypt
replace
encrypt again
UPDATE my_table
SET my_field = AES_ENCRYPT(REPLACE(AES_DECRYPT(my_field , 'KEY'), 'olddata', 'new data'), 'KEY')
WHERE AES_DECRYPT(my_field , 'KEY') LIKE '%olddata%'
Here is dbfiddle demo

MySQL: How do I search and replace chars at the beginning of a string

I'm trying to search and replace mobile numbers with the full international code.
So where rows have 07970000007 to replace the beginning with +447970000007
UPDATE tblMemberImportClub
SET msisdn = REPLACE(msisdn, '07', '+447')
WHERE INSTR(msisdn, '07') = 1;
But this also replaces the other matches:
+4479700000+447
I don't think i can use TRIM as some rows will already start with +447 and will therefore nor require any updates.
Thanks in advance for any assistance.
Use LIKE and INSERT():
UPDATE tblMemberImportClub
SET msisdn = INSERT(msisdn, 1, 2, '+447')
WHERE msisdn LIKE '07%';
INSERT() is a string function that replaces exactly the characters you specify (see here).
SELECT
CONCAT(
REPLACE(
LEFT('07970000007',2), '07', '+447'
),
SUBSTRING('07970000007', 3, CHAR_LENGTH('07970000007'))
)as replaced

How to replace only few characters from a string in mysql?

I have a field urn_sem.studentid that I'd like to replace a few characters in; for example:
ABC/2011/BCOMH_NC/I/12 → ABC/2011/BCOMH/I/12
ABC/2011/BCOMH_NC/I/24 → ABC/2011/BCOMH/I/24
I've tried this query:
SELECT REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
FROM urn_sem
but it doesn't show the new value.
Do you want this:
update urn_sem
set studentid = REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
where studentid like '%KNC/2011/BCOMH_NC/%'
The WHERE clause is optional. It ensures that the replace is only on rows that change.
And this sample query does not work?
SELECT REPLACE (studentid, '_', '') FROM urn_sem

MySql: updating a column with the column's content plus something else

I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'