Need help with this, as I am stumped with how to set the query update.
I will like to replace occurrences of 10x to 20x where x must be a numeric number. Also the numeric number can occur in any position. And the first two numeric digit of the numeric occurrence must be a 10
eg: tableA - at field colA has the followings:
TOK101s
102YUZ
TAIP103v
ECC10
ECC10a
SCC_103
TD-102b
ZA1104z
Result after the query update should be:
TOK201s
202YUZ
TAIP203v
ECC10
ECC10a
SCC_203
TD-202b
ZA1104z
ECC10 and ECC10a should not be updated since the 3rd char after 10 is not a numeric value.
ZA1104z should not be updated bec the numeric string 1104 does not begin with 10.
Thanks to Barmar and Misaka for:
SQL: search/replace but only the first time a value appears in record
update tableA set colA = IF(INSTR(colA, '10') <> 0, CONCAT(LEFT(colA, INSTR(colA, '10') - 1), '20', SUBSTRING(colA FROM INSTR(colA, '10') + CHAR_LENGTH('10'))), colA) where colA REGEXP '(^|[^0-9])10[0-9]'
The above helped me to get what I wanted.
Related
I have a table in mysql. Table Name is constitutive_table, it contains more than 40 columns and its type is varchar, it contains more than 25000 records. I wrote the query like this to get the 10 least value. But it showing like as you have seen in the picture.
SELECT `Sequence_Name`
, `Name_of_the_Protein`
, `Brain`
FROM `constitutive_table`
where `Brain` != 0
ORDER
BY cast(Brain AS int)
LIMIT 0,10
The data in the Brain column appears to be floating point, so you should be casting to the appropriate type:
SELECT Sequence_Name, Name_of_the_Protein, Brain
FROM constitutive_table
WHERE CAST(Brain AS DECIMAL(14, 8)) <> 0
ORDER BY CAST(Brain AS DECIMAL(14, 8))
LIMIT 10
Most likely what is happening now is that the 10 values you see all have the same value when cast to integer. As a result, MySQL is using some secondary sort to generate the order you do see.
While the above query may resolve your problem, ideally you should change the Brain column to some numeric type.
I am experiencing some weird behavior with MySQL. Basically I have a table like this:
ID string
1 14
2 10,14,25
Why does this query pull id 2?
SELECT * FROM exampletable where string = 10
Surely it should be looking for an exact match, because this only pulls id 1:
SELECT * FROM exampletable where string = 14
I am aware of FIND_IN_SET, I just find it odd that the first query even pulls anything. Its behaving like this query:
SELECT * FROM exampletable where string LIKE '10%'
When you compare a numeric and a string value, MySQL will attempt to convert the string to number and match. Number like strings are also parsed. This we have:
SELECT '10,14,25' = 1 -- 0
SELECT '10,14,25' = 10 -- 1
SELECT 'FOOBAR' = 1 -- 0
SELECT 'FOOBAR' = 0 -- 1
SELECT '123.456' = 123 -- 0
SELECT '123.456FOOBAR' = 123.456 -- 1
The behavior is documented here (in your example it is the last rule):
...
If one of the arguments is a decimal value, comparison depends on the
other argument. The arguments are compared as decimal values if the
other argument is a decimal or integer value, or as floating-point
values if the other argument is a floating-point value.
In all other cases, the arguments are compared as floating-point
(real) numbers.
how can i get the maximum value of my column that is a varchar with these values
for example i have a fieldname of myid which is varchar and what i want is to get the maximum value of the myid field . How can i query to get the 1-10 value of myid column?
myid
1-1
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-10
1-9
I would suggest using this trick:
order by length(myid) desc, myid desc
This will work for the data in the question. A more general answer is:
order by substring_index(myid, '-', 1) + 0, substring_index(myid, '-', -1) + 0
For the data you've shown:
select myid
from data_table
order by cast(substr(myid, 3, 2) as int) desc
limit 1;
In this case the ordering function is the integer value of the portion of the identifier following the dash. In general--i.e., for different or more complex data--you simply need to determine what the appropriate odering function is.
My S_ID values are
S_1
S_2
S_3,...., S_11.
I use SELECT MAX(S_ID) FROM stock_detail to get the maximum value.
It is working till S_9 but when it reaches S_11: that query only give me S_9 as the maximum value. How can I do to get S_11 as the maximum value ? Please help me, I am just a beginner in programming.
You can get the maximum value by using this construct:
select s_id
from stock_detail
order by length(s_id) desc, s_id desc
limit 1;
This puts the longer values first.
If you want to use max(), then you need to deconstruct the number. Something like:
select concat('S_', max(replace(s_id, 'S_', '') + 0))
from stock_detail;
This allows you to get a numeric maximum value rather than a character maximum value, which is the root of your problem.
I have a csv file that contains phone numbers, some of them have 9 digits and some of them have 10. Is there a command that would allow the transformation of the column such that numbers that have only 9 digits will have a 0 appended in front of the numbers.
For example,
if the column has values "443332332" and "0441223332", I would like to have the value of the one with 9 digits changed to "0443332332"?
Sorry, I should have elaborated.
I was wondering if there was a command to do it in SQLlite easily? I prefer not to use excel to transform the column as if I can get it to working with sqllite it would be so much easier and faster.
A more generic solution would be:
select substr('0000000000'||'1234567', -10, 10) from table_name;
The above query would always return 10 digits and add leading zeroes to the missed out number of digits.
For example, the above query would return : 0001234567
For Update, use
UPDATE TABLE_NAME SET PHONE_NO = substr('0000000000'|| PHONE_NO, -10, 10);
If you're sure that just prepending a zero on strings with length 9 will work for your application, something simple will work:
SELECT CASE WHEN LENGTH(phone_number) = 9 THEN '0'||phone_number
ELSE phone_number
END AS phone_number
FROM your_table
;
You could also update the table, depending on your needs:
UPDATE your_table
SET phone_number = '0'||phone_number
WHERE LENGTH(phone_number) = 9
;
Open the .csv using Excel,
Add a filter to the column,
Sort from A-Z to get all the columns with 9 digits,
Then follow the steps here
http://office.microsoft.com/en-au/excel-help/keep-leading-zeros-in-number-codes-HA010342581.aspx