Updating column with Replace - mysql

Using MySQL (MariaDB 10.4.12)
I am trying to update a column using the replace function. I need to replace an ip address with a URL. The value in the column is very long.
I am using:
UPDATE myTable
SET myColumn = REPLACE(myColumn, '192.168.9.1', 'www.mydomain.com/content')
WHERE ID = 1234;
When I run this, the value it updates to is very wrong.
However, if I remove the"/content" from the replace function, it updates correctly.
The data type of the column is longtext:
# Field, Type, Null, Key, Default, Extra
'myColumn', 'longtext', 'NO', '', NULL, ''
The value in this column is metadata, that is a very long string of characters/code. In the middle is my url that I need to update.
Example: a:4:{s:5:"child";a:1:{s:0:"";a:1:{s:3:"rss";a:1:{i:0;a:6:{s:4:"data";s:3:" and on and on and on.
Additionally, if I just select with the REPLACE() function, it returns the correct value.
Is there an issue with having a forward-slash ("/") in the replace function or update statement?
Thanks
-M

Related

Choose which field based on IFNULL

I have the following query which I am trying to run in MySQL. Basically I am trying to work out how to update the corresponding field which isn't null. I know its going to be one of two possible fields but I want to update the one which isn't empty. Maybe I am missing something blindingly obvious, but this is what I've tried thus far:
UPDATE `table`
#SET IF(FieldA IS NULL,FieldB,FieldA) = 1234
#SET IFNULL(FieldA,FieldB) = 1234
WHERE `FieldC` = '5678'
AND (
`FieldA` = '1234'
OR `FieldB` = '1234'
)
I suspect there may be a CASE solution but I'd prefer a shorthand/simple version option if it exists.
(My)SQL has no syntax that allows you to dynamically change the column you want to update, e.g. something like update ... set {PickFieldBaseOnCondition:FieldA|FieldB} = 1234. You have to specify a column there. The moment you start your query, the basic structure of the query (and all fields involved) have to be clear and fixed, only the values can change.
So you need to update both fields in your query if it shall be able to modify two different columns. But you can of course decide to just not modify the content of a field based on its content, e.g. keep it if it is null already:
update `table`
set FieldA = IF(FieldA IS NOT null, 1234, FieldA),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
Note that the requirement "update the other field if a field is null" only works if your initial condition that one field is null and one is not null is fulfilled, which you said is given. Otherwise, you should include a test if both fields are null or both fields are not null (in the comparison for the first field), which you could e.g. do with
update `table`
set FieldA = IF(FieldB IS null, 1234, null),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
FieldA can now be changed from content to null if both fields are not null (and from null to content if both fields are null), to enforce the condition that exactly one field is not null.
Please also note that IF() is a MySQL-only shorthand for CASE and doesn't work in all databases. You prefered a non-case solution, but it can trivially be rewritten using the sql-standard CASE.

Trim leading and update in mysql

I wanted to create a query in MYSQL that would trim the leading '# ' from all of the fields in a column.
update values set value = TRIM(LEADING '# ' from value)
However, doing this gives me an error
Duplicate entry '3002' for key value
The value column has a unique constraint and the error probably occurs because the query is trying to set the same value to all of value column after trimming.
Is there a way to do trim leading and update in mysql?
This query looks fine.
The issue might be here. Let me give you an example.
Case 1:
At row-x, you have value '#info' and at row-y you have info.
You removed the # from row-x in your query. Now you have already info value at row-y. You can not update the new value to info as it is there already.
I would suggest either to remove the UNIQUE constraint or you do not update the database itself. You can trim with your backend programming.
This is not setting all values to the same value, this is where once trimmed you now have two different rows with the same value for that column.
To find this:
SELECT id, value, TRIM(LEADING '# ' from value) AS trimmed_value ORDER BY trimmed_value
Presuming you have some kind of id column you'll be able to find any rows where trimmed_value is identical.

Strip dots from IP addresses in Mysql

I need to strip dots from a columns of IP addresses in MySQL table 'visits', I used the following query, what's wrong with it?
UPDATE 'visits' SET 'IP' = REPLACE('IP', '.', '');
Thanks
Use backticks ` instead of single quotes ' for table and column name
UPDATE `visits` SET `IP` = REPLACE(`IP`, '.', '');
That said, this method may create issues.
For e.g. you have two IPs: 10.1.1.11 and 10.1.11.1
After your update, both will become - 101111 and there is no way to tell which is which.
As #Alex said in the comments, if you want to represent the IP as numeric value, consider INET_ATON() instead, which returns an integer that represents the numeric value of the address in network byte order (big endian).
UPDATE `visits` SET `IP` = INET_ATON(`IP`);
It'll return unique number for an IP.
10.1.1.11 - 167837963
10.1.11.1 - 167840513

mysql syntax for populating a column of an existing table and setting a default value for said column

I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?

concat to text field

I have a table with 1 column "textField". Our version of mysql won't allow for full text fields to have a default value, the default is null.
When we want to update our table we want to append a value to whatever is in textField, like so:
update table set textField = concat( textField ,'value')...this works when there is something already in the field, but won't work if the field is null. Since mysql won't allow for a default value in a full text field the above statement won't work.
A solution is to do 2 queries:
1. check if that field is null
2. if so, then don't do a concat & just update w/ the value...if not null then do the concat
We'd rather not do 2 queries if we can avoid it...is there a one-liner alternative that would work?
Try this:
update table set textField = concat(COALESCE(textField, '') ,'value')...
concat(ifnull(textfield, ''), 'value')