MySQL length() not giving correct number of characters - mysql

I have a table with 8 columns, all varchar (24, 24, 24, 5, 5, 255, 255, 255).
The three important columns are:
icd10code (varchar 24)
icd10codedot (varchar 24)
icd10length (varchar 5)
I wish to convert one column that looks like this - "ABCDEF" to this - "ABC.DEF".
Here is the MySQL query used, and it worked perfectly!
UPDATE icd10
SET icd10codedot =
CONCAT ( LEFT(icd10code, 3) ,
"." ,
RIGHT( icd10code, ( LENGTH(icd10code)-3 )
)
);
All 91,000 rows came out nice EXCEPT there was a trailing "." at the end of those entries that only had 3 characters - like this: "ABC.".
I don't want that dot at the end.
So I tried this:
UPDATE icd10 SET icd10codedot = LEFT(icd10codedot, 3) WHERE LENGTH(icd10codedot)=4;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and none of them worked (a bunch of other combinations were also run, but none worked).
So to see how things were doing in the code, I made a column called "icd10length" varchar int and did this query:
UPDATE icd10 SET icd10length = LENGTH(icd10code);
and all I got was 91,000 "7"s lined up on the right of the column.
I changed the column from INT to VARCHAR 5, and reran the query and got 91000 "7"'s lined up on the RIGHT of the column ( :-) ).
So two questions:
How can I get the correct length of the variable icd10code or icd10codedot into a column?
Do you have a slick way of clipping the last character of a string that is 4 characters long?
Thanks very much again!
PS - I tried the initial query in PHP and it crashed - timing out after 300 seconds. Found on the web that you should always do things in MySQL whenever possible.
PSS - I'm doing all of this via phpMyAdmin.

It seems you have empty space at the end, try using rtrim.

Related

Left function leaving out characters

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.

SQL Like statement with regular expressions

My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FélixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'

Delete last character and add new two characters in MYSQL

I would like to delete last character from words from one column in MYSQL and directly add two new to the same word. For example I would like to change beiðni to beiðn|i.
Here is my query = UPDATEds_1_headword_backup1SETstem=
CONCATE((SUBSTRING(stem,1, ,LENGTH(stem) - 2), '|i') WHEREstem
LIKE '%i' ANDgram_1_word_group= 'f' ANDgram_2_endings=
'indecl'
Try something like this:
UPDATE ds_1_headword_backup1 SET stem = concat(substring(stem,1,length(stem)-1), '|i');
Example: SQLFiddle
Here is complete sql query:
UPDATE ds_1_headword_backup1 SET stem = CONCAT(SUBSTRING(stem,1,CHAR_LENGTH(stem) - 1), '|i'), gram_2_endings = '(-i)' WHERE stem LIKE '%i' AND gram_1_word_group = 'f' AND gram_2_endings = 'indecl'
The difference is in using char_length
I agree with chejnik, but don't have enougth reputation to comment his variant, so create another one.
You should use CHAR_LENGTH instead of LENGTH, because CHAR_LENGTH returns count of symbols, but LENGTH - sum of bytes.
SQLFiddle
So, 7alhashmi answer is correct only for strings, which have 1 byte per character.

MySQL - is it possible to update only part of value from current column?

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.

MySQL : Problem in Updating a column for condition

I have one phone field column which contains phone numbers like that
'123456789'
'123-456-789'
etc
means it contain 9 digit number or number + hyphen.
I want to make a SQL query which updates all records in 'xxx-xxx-xxx' format.
I have made few attempts but cannot get exact solution.
Please any one help me.
Thanks in advance.....
use something like
UPDATE mytable SET phone =
CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 3))
Also to only get the rows that are missing hyphens you would say WHERE phone not like '%-%'
Close. You'd first need to test if the string contained a '-' before adding more, but that's the right track.
UPDATE mytable
SET phone = CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 4))
where INSTR( phone, '-' ) = 0;