I have ONE column in MySQl table which contains words in this format:
Name - Name2 - Name3
and I'd like to delete Name2, and to leave just Name - Name3
So I just need to delete middle word, and not the entire column! The middle word is always the same.
Is that possible?
update your_table
set your_column = concat(SUBSTRING_INDEX(your_column, '-', 1), '-',
SUBSTRING_INDEX(your_column, '-', -1))
SQLFiddle example
substring-index doc
The easiest way is to use REPLACE.
UPDATE table SET column=REPLACE(column,'Yourstring','')
Suppose I have a table name car, and inside this table, there is a column called version. In this version, there are few data which has the string 'Auto', and I want to remove this.
GT 1.4 Turbo MultiAir 2d Auto -> GT 1.4 Turbo MultiAir 2d
So, the simple solution will be:
UPDATE cars SET version=REPLACE(version,'Auto','')
It will work for every occurrence.
Related
I have a column in my table that contains 10-digit hts codes (0000.00.0000). Some of the values do not have the full stop points (0000000000). How can I add the full stop points to all the rows that do not have them?
Edit
The column type is VARCHAR
I want to update all rows where full stop is not present.
I would remove the full stops from all these columns using REPLACE() as part of the update, then you can apply some simple logic using a CONCAT() LEFT(), RIGHT() and SUBSTRING()
to change the simple 0000000000 into 0000.00.0000 like this, rather than trying to identify only the columns without the dots
UPDATE table
set column = CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
);
Test it using a select so you do no damage
SELECT some_identifying_column,
CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
) as justtesting;
Another approach using insert comes to mind. As others already mentioned, it's a good idea to remove the full stops before inserting them in the 5th and 8th position in the string
select *, insert(insert(replace(hts,'.',''),5,0,'.'),8,0,'.')
from t;
Good Evening Everyone;
I have a table in MySql that I would like to update as follows. Just as an FYI I have searched this topic and have yet to find a solution.
The table name is ALK_Results_NEW in a MySQl database, the data in the ICD9 column contains data seperated by commas.
I need to keep the first set in the ICD9 column so for example the first row has V57.9 , 246.9.
I need to keep the V57.9 in the ICD9 column and move 246.9 into the ICD9_SECONDARY column.
If a row has more than 2 then I need to move all other into the ICD9_OTHER column (This column can have mulitple ICD9 Codes separated by commas)
To summarize the first code needs to stay in the ICD9 column and the second set of codes needs to be moved into the ICD9_SECONDARY. After the data is moved the ICD9 Column should only have the forst set of codes.
Any help would be greatly appreciated.
Assuming the two columns already exist in the table, you can change the data using an update:
update alk_results_new
set icd9_secondary = substr(icd9, instr(icd9, ',') + 1),
icd9 = substring_index(icd9, ',', 1)
where icd9 like '%,%';
EDIT:
Oops, I didn't realize there were three columns. The approach is similar, but a little more complicated because you need to take into account the length of the strings. I think the following should do what you want:
update alk_results_new
set icd9_other = (case when icd9 like '%,%,%'
then substr(icd9, length(substring_index(icd9, ',', 2)) + 2)
end),
icd9_secondary = (case when icd9 like '%,%'
then substring_index(substring_index(icd9, ',', 2), ',', -1)
end),
icd9 = substring_index(icd9, ',', 1);
Note: test the logic out on a select before running the update.
I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
I have the following imported to TableA, Column 'Clothes' and Column 'Colours'
The problem is the import has put in the 'Clothes' column 'Jeans - Blue' and 'Jumper - Red' etc etc
Please could someone help me with a query to keep everything before the - in 'Clothes' and everything after the - into 'Colours' and removing the - altogether.
Two steps for this.
First, update the colors:
UPDATE yourTableA T
SET T.Colours = TRIM(SUBSTR(T.Clothes,INSTR(T.Clothes,'-') + 2));
Second, update the Clothes:
UPDATE yourTableA T
SET T.Clothes = TRIM(SUBSTR(T.Clothes,1,INSTR(T.clothes,'-')-1));
I've used SUBSTR as my string swiss army knife here, and INSTR to locate the position of the - in between. You can do without TRIM, but I usually use this in those cases to avoid unnecessary white spaces.
There surely are more direct ways to do it, but this'll work.
The SUBSTRING_INDEX function is convenient, and the TRIM function can remove leading and trailing spaces. For example:
SELECT TRIM(SUBSTRING_INDEX(a.Clothes,'-',1)) AS Clothes
, TRIM(SUBSTRING_INDEX(a.Clothes,'-',-1)) AS Colours
FROM TableA a
WHERE LENGTH(a.Clothes)-LENGTH(REPLACE(a.Clothes,'-','')) = 1
(NOTE: the query above is returning the substring before the first '-' character, and is returning the substring after the last '-' character. So any values with more than one dash would lose the portion between the first and last dashes, consider e.g. 'A - B - C - D', the query above returns the A and returns the D, and loses everything else.
To handle this anomaly, the WHERE clause checks that the string contains a single occurrence of the '-' character.
Once you have a query you are happy with, you can turn that into an UPDATE statement, BUT be VERY careful about the order you assign new values to columns. Unlike other relational databases, MySQL does not guarantee that a reference to an existing column within the statement will be the value of the column from the beginning of the statement... the only guarantee is that it will be the value that is currently assigned. So, the order that the columns is assigned is important!
UPDATE TableA a
SET Colours = TRIM(SUBSTRING_INDEX(a.Clothes,'-',-1))
, Clothes = TRIM(SUBSTRING_INDEX(a.Clothes,'-',1))
WHERE LENGTH(a.Clothes)-LENGTH(REPLACE(a.Clothes,'-','')) = 1
Note that if we were to assign the Clothes column before we assigned a value to the Colours column, the value we want assigned to Colours would be "lost".
You can do it in a single UPDATE as follows:
UPDATE TableA
SET `Colours` = SUBSTRING_INDEX(`Clothes`, ' - ', -1),
`Clothes` = SUBSTRING_INDEX(`Clothes`, ' - ', 1)
;
You can experiment with SQL Fiddle Demo I created from your data.
Here's the data I worked with:
CREATE TABLE TableA
(Clothes varchar(20), Colours varchar(20))
;
INSERT INTO TableA
(`Clothes`, `Colours`)
VALUES
('Jeans - Blue', NULL),
('Jumper - Red', NULL)
;
This the result of SELECT * FROM TableA; :
CLOTHES COLOURS
Jeans Blue
Jumper Red
I have table A and B with many to one associations (b contains fk_a). Let's assume the sample tables are as follows:
A:
id first
1 sample
2 sample
B:
id fk_a type value
1 1 som thing
2 1 oth other
3 2 som thing
4 2 oth any
I would like the "first" column in table A to be unique, and I would like to achieve it by having to:
desired A:
id first
1 sample-thing-other
2 sample-thing-any
Is it possible to use pure MYSQL to use UPDATE and CONCAT on table A to obtain desired update?
It would be easy if I had everything in one table, I could just write
UPDATE A
SET first = CONCAT(first, value)
but unfortunately I have many-to-one association and I am not sure if it is even possible in such case.
I do not have the instance of mysql, and not test it. use the group_concat
UPDATE A, (SELECT fk_a, GROUP_CONCAT(value SEPARATOR '-') as concat_value FROM B GROUP BY fk_a) AS t
SET A.first = CONCAT(A.first, '-', t.concat_value)
WHERE A.id = t.fk_a;
Group_Concat is your friend.
But may I remind you of first normal form?