Mysql REPLACE resulted in 0 values on all rows - mysql

I've used the following SQL query in a MySQL database to replace part of a string in a cell:
UPDATE TEST.database2014 SET together1 = REPLACE(together1, "/1900", "20")
For some reason all the rows (225,000!) have now a value of 0.
This is the message which I got:
/* Affected rows:225,000 Found rows: 0 Warnings: 0 Duration for 1 query: 16,888 sec. */
ADDITIONAL INFORMATION:
data example contained in field together1:
TESTING^^^19/01/2014^^
Is there a known reason for this happening?
I find it strange that if no matches where found it converted all values to 0 anyway.

I think that you must use this:
UPDATE TEST.database2014 SET together1 = REPLACE(together1, "/19", "/20") WHERE togheter1 LIKE '%/19%'
if you want to upate all year 1900 to 2000

Related

SAS : Eliminate duplicates if a condition is satisfied

I want to eliminate duplicates from a database, based on an identifier, an order and a condition.
More precisely, I have data with several observations. I have sometimes a condition that makes me want to keep that observation anyway (let fix it condition=1), but then also keep the observation with the same identifier even if this condition does not hold (condition=0).
But if I have for one identifier several observations where condition=0 then I want to elminate duplicates, with criterion being having the greatest order.
Without the condition I can do that
proc sort data=have;
by identifier descending order;
run;
proc sort nudopkey data=have;
by identifier;
run;
But how to incorporate my condition in this ?
Edit 1 : add a database example :
data Test;
input identifier $ order condition;
datalines;
1023 1 0
1023 2 0
1064 2 0
1064 1 0
1098 1 0
1098 1 1
;
Then I want to keep
1023 2 0
1064 2 0
1098 1 0
1098 1 1
Edit 2 : tried to precise my conditions
I presume you want to eliminate duplicates only when the condition for all records for an identifier is set to 0. In that case you want to keep the record with the maximum order and eliminate all other records with the same identifier.
Proc sql;
create table want as
select *
from test
group by identifier
having max (condition) ne 0
or order eq max (order)
;
Quit;
This will keep all rows for an Identifier where the maximum condition = 1,
or in the case of those where maximum condition = 0, select the row with the maximum order.
Is that what you want?
Some of this depends on how you define 'condition'. Is your condition easily verifiable on every record for that identifier? Then you can do something like this.
Evaluate the condition.
For records where it is true (you want to remove the duplicate), set flag=0. For records where it is not true, increment the condition flag by one.
If the condition is true for all records in that ID, all will have the same value (flag=0) and nodupkey on by identifier flag; will remove extras. If the condition is false for all records, those will not be removed. If it's true for some and false for some, and you want to remove only some of the records with that identifier (only the duplicates where it is true), then you have to make sure that either it's sorted to have all of the condition=true records at top, or have a separate flag counter that determines what value the flag will be (since it sometimes will go to 0 in the middle, so 0 0 0 1 2 3 0 4 5 6 is what you want, not 0 0 0 1 2 3 0 0 1 2 ).
Perhaps easier to see is to do it within a datastep. After sorting by identifier descending order:
data want;
set have;
by identifier descending order;
if (condition=true) and not (first.identifier) then delete;
run;
This will, again, work if either condition=true is always at the top, or if it's always consistent within one ID group. If it's inconsistent and mixed, then you need to keep track of whether you've kept one where it was true (assuming you want to), or it might delete all records where it is true; use a separate variable to keep track of how many you've kept. first.identifier will be 1/TRUE for the first record for that identifier only, not taking into account the condition. You could also create the flag, then sort by identifier flag descending order; and guarantee the condition=true are at the top (either by making flag=0 for true, or sorting by descending flag.)

Update anomaly. Mysql sql different rows affected count

SELECT * FROM `attempts` WHERE date = '27-04-2014' LIMIT 0 , 30
This particular query gave 386 results(PHPmyAdmin) but on executing the below query
UPDATE `attempts` SET points = points *2 WHERE date = '27-04-2014'
I got 379 rows affected. . Shouldn't I get same numbers? Any other reasons possible? Or am I wrong somewhere?
The query won't affect the rows where points = 0, because doubling the value of points won't have any effect.
For example, try running this query:
UPDATE `attempts` SET points = points + 0 WHERE date = '27-04-2014'
and it will show 0 rows affected.
Also, the count shown by phpMyAdmin is an estimate, if you're using InnoDB. Use COUNT(*) to get the exact count.
SELECT COUNT(*) FROM `attempts` WHERE date = '27-04-2014'
"Affected rows" counts only rows that were changed. If you have records with 0 points, doubling the number of points has no effect and these records will not be included in the count.

MySQL - replacing specific character inside of column string

Got a column that keeps strings like such:
{"plan-36";"id-36";}
I want to check each string of this column, and if matches my criteria, it would change a specific character of this string (for example would change number 36 to 99). Here's what i got so far:
SELECT string_column
FROM example_table
WHERE
INSTR(string_column,'plan-36') > 0
OR
INSTR(string_column,'id-36') > 0
This only returns rows that has 'plan-36' or 'id-36' string in them. What i need is - if this row contain such strings, i need to change the 36 value, to let's say 99.
Which sql functions do i need for this?
UPDATE example_table
SET string_column = REPLACE(string_column, "36", "99")
WHERE
INSTR(string_column,'plan-36') > 0
OR
INSTR(string_column,'id-36') > 0
would be the syntax, this will update all instances of 36 to 99, Be careful and test on backup data first.
It may be better to use this
SET string_column = REPLACE(string_column, "-36", "-99")
which includes the dash

MySQL equal operator truncates values and does not work as expected

I've just discovered a strange behaviour of MySQL, about the equal operator.
In my table example, I have a row with id = 914.
When I run the following request:
select * from example where id='914z';
MySQL returns the row with id = 914.
Why???
I mean... '914' is NOT '914z' !
I believe that this is completely not normal.
If you don't, please tell me why, I'd be happy to discuss.
It works exactly as it should, and the behaviour is documented. Documentation has good examples:
The following examples illustrate conversion of strings to numbers
for comparison operations:
mysql> SELECT 1 > '6x'; -> 0
mysql> SELECT 7 > '6x'; -> 1
mysql> SELECT 0 > 'x6'; -> 0
mysql> SELECT 0 = 'x6'; -> 1

Can't update table in MySQL, I am using the workbench

I am a fairly new user to MySQL although I am fairly experienced with SQL (DB2 environment). I am using the workbench to run queries and update statements. I am having a problem updating data in a table which I have been able to prior. I'm able to select rows but when I go to update based on the same criteria, the return message is:
**0 row(s) affected Rows matched: 9 Changed: 0 Warnings: 0**
Update gina1.proj001_bcbs set contract_percentage = 1.50
where contract_category = 'All Other Services'
and doctor = 'JSmith' ;
When I run the same WHERE clause with a select I get the correct list of records.
**9 row(s) returned** and I get the correct list of data.
select * from gina1.proj001_bcbs
where contract_category = 'All Other Services'
and doctor = 'JSmith' ;
I do not believe I am logging but I can't say for sure, I did ready somewhere about resetting the log. If someone can help it would be great.
This means simply, that all relevant records already have contract_percentage = 1.50
0 row(s) affected : No rows were affected by your query
Rows matched: 9 : 9 rows were found, ...
Changed: 0 : ... but none of them had to be changed
Warnings: 0 : Nothing recoverable bad happened while runinng the query
.
Update gina1.proj001_bcbs set contract_percentage = 2.50
where contract_category = 'All Other Services' and doctor = 'JSmith' ;
Is ver likely to bring you 9 row(s) affected Rows matched: 9 Changed: 9 Warnings: 0