Using Substring and like together - mysql

Hi I want to extract all values from the column Old_priceplan such that the outcome gives me 1 Mbps only
So if its xyz 20 Mbps it extracts 20 Mbps only and so and so forth.
also
is there a way i can use or in locate like locate('Mbps' or 'MB', old_priceplan?)

substring(old_priceplan, locate('Mbps',old_priceplan) - length(old_priceplan) - 4 , ifnull(locate('SL',old_priceplan),0) )

You can build a proper like condition eg:
assuming you value is store in a var
set #str = '1 Mbps';
then
select Old_priceplan
from my_table
where Old_priceplan like concat('%', #str, '%')
And as suggested by Lutz be sure you have a proper sanitized value assigned to var

Related

Compairing two strings in MYSQL

I have a sequence of 20 numbers from 0 to 2, I want to compare this string with other sequences saved in my database, the problem is that the lenght of the strings saved on the database fluctuates.Also the comparison needs to be done from the end to the start.
Example of what I want:
20 digits string:
'1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1'
couple of strings saved in the database:
1 - '1,1,2,1'
2 - '2,1,2,2,2,2'
3 - '2,1'
4 - '1,1,2,1,2,1'
In this case the query would return the 1 and 3 only
create table mytable ( s varchar(60) );
insert into mytable values
('1,1,2,1'),
('2,1,2,2,2,2'),
('2,1'),
('1,1,2,1,2,1');
set #x = '1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1';
select s from mytable
where right(#x, length(s)) = s;
Output:
s
1,1,2,1
2,1
Fiddle: https://www.db-fiddle.com/f/r5m2hPbnmUu5VQfYvMVtir/0
You could use a LIKE trick here. For example, to check for the first string 1,1,2,1:
SELECT *
FROM yourTable
WHERE ',1,1,2,1,2,1,0,1,2,1,2,1,0,1,2,1,1,1,2,1,' LIKE '%,1,1,2,1,%';

Advanced MySQL pattern matching (beyond LIKE...%)

This is the example of my current MySQL my_table...
id name code
1 111 XXXX123456XXXXXXXXXXXXXX
2 222 XXXX133456XXXXXXX5XXXXXX
3 333 XXXX123454XXX11XXXXXXABC
Code is a 24 character hexadecimal value where X is the wildcard.
I need to write a query that will return the NAME based on the CODE without a wildcard value X.
The given CODE value will be exact but it should compare the string in place, X could match any character.
For example:
SELECT name FROM my_table where code = '012312345611111111111111';
name
111
SELECT name FROM my_table where code = '000013345622222225123456';
name
222
SELECT name FROM my_table where code = '000123454ABC11234567FABC';
name
333
You can use like for this. Are you aware of the _ wildcard?
select t.*
from t
where #YourCode like replace(t.code, 'X', '_');
Of course, you can use regular expressions too. The regular expression would be: concat('^', replace(t.code, 'X', '.'), '$').

Patern maching like Select Query

I have a table that contains this kind a structure of a column, how can I make select only from character 4 to 6 to ignore other character that are outside this boundary , I tried LIKE'%544%', RegExp. ect. ??
1 000544001
2 000054400
3 000544010
4 000344010
5 000544011
One way is to use substr():
where substr(col, 4, 3) = '544'
another is to use like:
where col like '___544%'
you can also use mid(col,start,length) statement
like this
select column1 from table1 where mid(column1,4,3);

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

How to store and then search through CSV in sql?

I know that I can use IN(1,2,3) to select items from an SQL database where their value is either 1, 2 or 3, but what if I have a value in MYSQL which is a string, and is equal to '1,2,3' and I want to select that value. As far as I know I have to do this:
SELECT whatever FROM wherever WHERE sqlvariable = '1' OR sqlvariable LIKE '1,%' OR `sqlvariable` LIKE '%,1,%' OR `sqlvariable` LIKE '%,1'
which is fine if I want to simulate 'IN' sort of in reverse. But lets say I want to select sqlvariable where it contains either 1 or 2, then I have to do this:
SELECT whatever FROM wherever WHERE sqlvariable = '1' OR sqlvariable LIKE '1,%' OR `sqlvariable` LIKE '%,1,%' OR `sqlvariable` LIKE '%,1' OR sqlvariable = '2' OR sqlvariable LIKE '2,%' OR `sqlvariable` LIKE '%,2,%' OR `sqlvariable` LIKE '%,2'
Now lets say I use PHP to generate the above queries and I'm looking to find sqlvariable where it contains a 1 2 4 66 34 33 22 44 or 992. It's going to take an awfully long time for this query to run.
I could do a bitwise implementation, but then I'm limited to only having about 60 different items or so which isn't enough.
Is there a faster way to utilize a CSV query like the above in sql? I suppose it's sort of like IN() but in reverse.
You can simplify your queries using FIND_IN_SET:
SELECT whatever
FROM wherever
WHERE FIND_IN_SET('1', sqlvariable)
OR FIND_IN_SET('2', sqlvariable)
This however will still require searching every row in the table. It would be better to redesign your database so that CSV formatted data is not required.
Update: Your existing design looks something like this:
groupname groupmembers
Group1 1,2,3
Group2 3,4
You should change it to something more like this:
groupid groupmember
1 1
1 2
1 3
2 3
2 4