Split field values using specific character by mysql - mysql

sorry for the repeated question. I already asked for help about this by the use of ORACLE database. But now I really wanted to know how can I split this using MySQL database
It is possible to split field values using a specific character? Here is my sample table
value
10uF
2K
1.0uF
200UF
I want it to split by this:
value capacitance/resistance
10 uF
2 K
1.0 uF
200 UF
Hope you can help me once again. Thanks! and more power!

You can use below code
create table temp
(value varchar(10)
);
insert into temp values ('10uF');
insert into temp values('2K');
SELECT value + 0 AS num
, REPLACE(value,value+0,'') AS unit
FROM temp
O/P
num letter
10 uF
2 K
The trick the query is using is to evaluate the column value in a numeric context, by adding a zero. MySQL will return the numeric value.
But this wont work in case of 10Uf10,2k3..
Hope all your data is digit + charachter
Fiddle for the same

Related

Select records from a table where one column value lies in another column in the same table using SQL [duplicate]

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed last month.
I want to select rows from a table where a column value lies in another column value of the same table.
I am trying to execute code in Oracle SQL developer
I have a larger input dataset similar to below :
keys
val
set
1
H
H, L , M
2
L
P, Q , R
3
P
P, S
I want to select only those records where my val column value lies in the set column of the same table. There can be multiple or single values in the set column
The output would be like this :
keys
val
1
H
3
P
How to achieve it using MySQL ? Please help.
**Note **: I have tried using below code. Its not working :
select keys, val
from a where val in (set)
you can do a simple comparison using concat :
SELECT `keys`, val
FROM dataset
where `set` like concat('%',val,'%');
Try it here : https://dbfiddle.uk/HZ5spY-k
I think it's need something like:
SELECT keys, val FROM a WHERE SET='val'
I think you should try this
SELECT `keys`, `val` FROM `temp` WHERE FIND_IN_SET(`val`, `set`);

MySQL Virtual Column Wrong Values

i really loves mysql virtual generated columns but am having a small issue with it am trying to make a virtual column from a varchar column where i extract the number as the following.
DB::statement('ALTER TABLE reservations ADD number_vc BIGINT AS (REVERSE(REVERSE(number) << 0)) ');
my issue is in this part REVERSE(REVERSE(number) << 0))
as if you tried now to run this part of sql in any sql editor as the following
SELECT REVERSE(REVERSE("A100") << 0)
it will generate the following
**What Am Trying To Achieve **
if the string was A100 after reverse i need 100 also the schema of the varchar not always one character it may be like this
A100 , AA100 , AB100
**Edit 2 :Am Afraid i did go production a week a go and here is the result **
With mySQL8+ you can do this :
SELECT REGEXP_REPLACE('A100', '\D', '');
It's basically replacing non digits characters with nothing using a regexp.

MySQL In clause not giving the right result

In a MySQL table i have a field, containing this value for a given record : "1908,2315,2316"
Here is my sql Query :
SELECT * FROM mytable WHERE 2316 IN (myfield)
I got 0 results!
I tried this :
SELECT * FROM mytable WHERE 2315 IN (myfield)
Still 0 results
And then i tried this :
SELECT * FROM mytable WHERE 1908 IN (myfield)
Surprisingly i obtained the record when searching with 1908! What should i do to also obtain the record when searching with 2315 and 2316 ? What am i missing ?
Thanks
You appear to be storing comma delimited values in a field. This is bad, bad, bad. You should be using a junction table, with one row per value.
But, sometimes you are stuck with data in a particular structure. If so, MySQL provides the find_in_set() functions.
SELECT *
FROM mytable
WHERE find_in_set(2316, myfield) > 0;
You can't use IN() over comma separated list of no.s its better to normalize your structure first for now you can use find_in_set to find results matching with comma separated string
SELECT * FROM mytable WHERE find_in_set('1908',myfield) > 0
This question has been asked and answered before, but I don't want to hunt for it; this question should be closed as a duplicate. But, to answer your question:
The commas in the string, the column value, are just characters. Those are part of the string. They aren't seen as "separators" between values in the SQL text. The way SQL sees it, the column contains a single value, not a "list" of values.
So, in your query, the IN (field) is equivalent to an equals comparison. It's equivalent to comparing to a string. For example:
... WHERE 2316 = '1908,2315,2316'
And those aren't equal, so the row isn't returned. The "surprisingly" finding of a match, in the case of:
... WHERE 1908 IN ('1908,2315,2316')
that's explained because that string is being evaluated in a numeric context. That is, the comparison returns true, because all of these also true:
... WHERE 1908 = '1908,2315,2316' + 0
... WHERE 1908 = '1908xyz' + 0
... WHERE 1908 = '1907qrs' + 1
(When evaluated in a numeric context, a string gets converted to numeric. It just happens that the string evaluates to a numeric value that equals the integer value it's being comparing to.)
You may be able to make use of the MySQL FIND_IN_SET function. For example:
... WHERE FIND_IN_SET(2316,'1908,2315,2316')
But, please seriously reconsider the design of storing comma separated list. I recommend Bill Karwin's "SQL Antipatterns" book...
http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
In mysql IN clause is utilized as
SELECT * FROM mytable WHERE column_name IN (set_of_values) ;
Mention column name instead of values
Please try
SELECT * FROM mytable WHERE LOCATE(CONCAT (',', 2316 ','), CONCAT (',',myfield,',' ) ) <>0

SQL - Strip first digit if it starts with the number '9'

Basically I have a table called 'Telephone' with over 100,000+ records.
In this database I have numbers such as:-
90123456789
91234567893
97126372319
Basically I want to Update the Telephone.DB and SET the numbers so it strips the first number only if it starts with the number '9', so the result would end up as follows:-
0123456789
1234567893
7126372319
Any idea how I can achieve this using MySQL?
Provided that the datatype is a string type, this should do it;
UPDATE numbers
SET number = SUBSTR(number, 2)
WHERE number LIKE '9%';
An SQLfiddle to test with.
As always, test yourself before running updates from random people on the Internet on your production database :)
Ok, this works for a VARCHAR column. You could always convert on the fly the value to varchar:
SELECT CONCAT(REPLACE(SUBSTR(PHONEFIELD,1,1),9,''), SUBSTR(PHONEFIELD,2)) AS PHONEWITHOUTNINE FROM TABLENAME
Check it out and tell me if it works.

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).