I have a database with over a 1,000 numbers in roughly the same format: aaa-111-2222
Some of the numbers have extra parts at the end: aaa-111-2222(bbbb)
I created a column and updated my table using the left function:
UPDATE table_1
SET col_2 = Left(col_1, 12)
I want to update the entire table so I didn't include a WHERE function.
The problem is when it updates, it is leaving off two characters.
Col_1: aaa-111-2222(bbbb)
when the function was ran it returned:
Col_2: aaa-111-22
Which is short characters. I thought that there might be some leading spaces I couldn't see, so on the orginal column I:
SELECT REPLACE(Col_1, " ","") FROM table_1
Then I reran my update and it was still returning short. I thought maybe I had extra returns and other white space, so I ran:
SELECT REPLACE(REPLACE(Col_1, CHAR(13), ''), CHAR(10), '') FROM table_1
Then I deleted Col_2 and remade it again. The structure was VARCHAR, with a Length of 15 (Which is 3 more than I should have)
After I ran the UPDATE again, it is still short 2 characters.
Any ideas on what I else I can do to fix?
May be there are leading spaces in col_1. The REPLACE did not update col_1 as it only applies the resulting dataset of the SELECT. Try use TRIM to eliminate the leading and trailing spaces to see if it makes any difference:
UPDATE table_1
SET col_2 = Left(TRIM(col_1), 12) ;
Also try this:
UPDATE table_1
SET col_2 = LEFT(TRIM(REPLACE(REPLACE(col_1,'\r',''),'\n','')), 12);
I ended up noticing that my table was 3 times bigger than it should have been. I emptied the table, reloaded it with backup data I had, then did the REPLACE and after that it worked just fine.
Thank you guys for the tips.
Related
I have a MySQL table called table_1 that has column named col_1 of type TEXT. I am trying query a row that has a certain value using this statement, but no data is returned.
SELECT * FROM table _1 WHERE col_1 = '1325'
However, when I used this statement, I do get data.
SELECT * FROM table_1 WHERE col_1 LIKE '%1325%'
How do I get the first statement to work?
If the select with the WHERE col_1 LIKE '%1325%' brings back a value that looks like 1325, but then WHERE col_1 = '1325' doesn't?
Then it's probably because of invisible characters.
For example if the value of col_1 is '1325 '.
Because of the extra space, that value isn't equal to '1325'.
But it does contain '1325', so the LIKE would find it.
It's easy not to notice what's not visible.
What you could do is TRIM the value, so that spaces are removed before comparing it with =.
SELECT * FROM table_1 WHERE TRIM(col_1) = '1325';
Note that a default TRIM or RTRIM removes only the spaces.
To make that work for other whitespace characters the SQL becomes be a bit longer.
SELECT * FROM table_1
WHERE TRIM(Replace(Replace(Replace(col_1,'\t',''),'\n',''),'\r','')) = '1325';
And if you want to have visual evidence wether this is caused by invisible whitespaces?
Then use the LIKE, and CONCAT something to the start and end of the column. Then the spaces will be easy to notice in the result.
select CONCAT('[',col_1,']') AS Test, t.* from table_1 t where col_1 like '%1325%';
You can test it here
Because you do not test with:
- **SELECT * FROM table _1 WHERE col_1="1325" **
the second statment means the data contains 1325 but you don't have a row containning 13
Hope you got iT thanks
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 am trying to INNER JOIN two tables on a text field, but I can't get it to work.
I've backtracked to see if I can identify where my query is falling down.
I've simplified my query to use only one table, to see if I can select data from it based on a text string.
I can't make it work.
I feel like an idiot.
This is so basic, why isn't it doing it?
select * from `my-table`
where myKey = "some-text-data-that-i-copied-from-the-data-in-the-table";
Returns no rows.
"some-text-data-that-i-copied-from-the-data-in-the-table" is in field myKey. I can see it. It's in the table!!!
[if it makes any difference I am most familiar with using an Access front-end and I am porting my "skills" to MySQL because MySQL is just better really.. this seems like the most run of the mill SELECT statement ever and yet it doesn't do anything!]
EDIT:
IMSoP does this help any...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
EDIT 2:
Diego Marinelli - thanks - this returns data
select * from mymathssowlink
where MyMathsResourceKey like '%KS2%';
EDIT 3:
This works...
select * from mymathssowlink
where MyMathsResourceKey like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
EDIT 4:
But this still doesn't...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
puzzled
Table:
CREATE TABLE `mymathssowlink` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ObjectiveID` varchar(13),
`MyMathsResourceKey` varchar(100),
PRIMARY KEY (`ID`),
UNIQUE KEY `idnew_table_UNIQUE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
HOW IT GOT FIXED:
okay very weird but I have fixed the simplified problem now! I remember from my Access days that trailing spaces are the bane of matches and it seems the MySQL is a bit fiddly in this regard to. There was no problem with the data as such but the field MyMathsResourceKey was the last bit of data on each line and so not terminated with a comma. Some lines didn't have any data in that column and so were terminated with a comma rather than the MyMathsResourceKey data. I fixed it by adding a third column to my csv into which i uniformly stuck the string "PLACEHOLDER". I matched this to a column called placeholder in the table receiving the data. Now that the active (2nd column) is always terminated with a comma all the data seems to work. I can't help thinking that this is a bit odd and that MySQL data imports really don't want to be behaving like this... now to try an make my JOIN work, which how this all started!
Thanks to all, especially Rahul for the help.
... and the JOIN Works now...
Going by all the edits in your post and comments; what I feel is, there is extra space present in your column value and so you are not getting the data (since it's not matching the exact string).
So either try like
select * from mymathssowlink
where MyMathsResourceKey
like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
(OR)
Use a REPLACE() function to remove all spaces in the field value
select * from mymathssowlink
where REPLACE(MyMathsResourceKey, ' ', '')
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can as well use TRIM() function if you can guarantee that the spaces are present only in left/right extreme of the string.
select * from mymathssowlink
where TRIM(MyMathsResourceKey)
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can try Select * from table; and if that works then try select * from table where key like '%some_text%'
It could not be a problem with the sintax but with the query itself.
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
Here is an example table on which I would like to execute a query:
Structure of table_1
number | photos (CHAR,4)
1234 | 1210
I would like to update value from column photos, but without changing the whole value. I would like change, for example, only third character to "2" without knowing the whole value. How can I do that?
I know I could do that in this way described below, but the problem is the value may be variable and it is a column type CHAR, not INT.
UPDATE table_1 SET photos = (photos + 10) where number='1234'
Yes with mid, left and right functions because photos is a type char:
UPDATE table_1 SET photos =
concat(
left( photos, 2),
'1',
right( photos, 1)
)
where number='1234'
Use concat(), left() and substring()
The example above would look like this:
update table_1
set photos = concat(left(photos, 2), "2", substring(photos, 4))
where number = '1234'
The advantage to this over left/right, is this will work for variable lengths of "photos".
Looking at it more generally, if you want to set the xth position to "2":
update table_1
set photos = concat(left(photos, x-1), "2", substring(photos, x+1))
where number = '1234'
(NOTE: I don't have MySQL running right now so I can't test the above. There are certain to be off-by-one errors which should be easy for your to correct)
If the column contains only numbers then the example you gave would work. The value will be converted to an integer, added to, and then converted back.
You might want to explain your reasoning for wanting to do this though. It seems a bit strange.