MySQL - How To Query Results With Match Of Middle Text/Digits - mysql

Imagine a column in table with digits and text, from 1001 to fa00ty and even 01100001 or longer, there's millions of rows of data. I want to query the table and return rows with only "00" or maybe just "0" in the "exact middle of the text/digits of the column". Example table
|some00e |
|01100001 |
|fa00ty |
|m00ol |
|23_00_asd|
|some long string with 00 some long string with|
There are only two matching results for "00" which would be fa00ty as 00 is exactly center of this text and 'some long string with 00 some long string with' and two for the single 0 query. So odd or even length does not matter.

Personally, I'd go with something like this:
SELECT stuff
FROM test
WHERE
(substring(stuff FROM (0-length(stuff)/2)-1 for 2) = "00"
AND (length(stuff) % 2 = 0))
OR
(substring(stuff FROM (0-ROUND(length(stuff)/2)) for 1) = "0"
AND (length(stuff) % 2 != 0))
This will only return even numbers (i.e. so the exact middle can be two digits) whose middle digits are '00', or odd numbers whose middle digit is '0'.

Following query works much faster. Still unsure if it is the best answer as I am not query knowledgeable, thus awaiting other possible solutions. Also will not work with anything greater than 3 digits as far as I could test.
For 2 digits
MID(`stuff`,round(LENGTH(`stuff`)/2),2) = '00' AND LENGTH(`stuff`)%2=0
For 1 digit
MID(`stuff`,round(LENGTH(`stuff`)/2),1) = '0' AND LENGTH(`stuff`)%2

Related

Sql Query to fetch invalid entries

I want a sql query to fetch invalid entries in ph_no such as entries which has text(a-z) or special characters or which are not 10 digit long or entries which are 10 digit long but has special character or text in it
I have used the following code
SELECT PH_NO FROM Table WHERE LENGTH(PH_NO)<=9
It is only fetching entries which are not 10 digit long but i want entries which are 10 digit long but contains text or special character as well
You can try using regular expresion in your where clause, you get when PH_NO length is less than 10, or when the length is 10 and is not in the regular expresion:
SELECT PH_NO
FROM table1
WHERE LENGTH(PH_NO)<=9 OR
(LENGTH(PH_NO)=10 AND PH_NO NOT REGEXP '^[[:digit:]]{10}$')
This query will help you achieving the specifics given by you
SELECT PH_NO FROM Table WHERE LENGTH(convert(PH_NO,signed))!=10;
The conver() will return 1 or 0 if PH_NO contains any character, so if your 10 digit PH_NO will contain any invalid entry than this query will give the right output.

select int column and compare it with Json array column

this is row in option column in table oc_cart
20,228,27,229
why no result found when value is 228 but result found when value is 20 like below :
select 1 from dual
where 228 in (select option as option from oc_cart)
and result found when I change value to 20 like
select 1 from dual
where 20 in (select option as option from oc_cart)
The option column data type is TEXT
In SQL, these two expressions are different:
WHERE 228 in ('20,228,27,229')
WHERE 228 in ('20','228','27','229')
The first example compares the integer 228 to a single string value, whose leading numeric characters can be converted to the integer 20. That's what happens. 228 is compared to 20, and fails.
The second example compares the integer 228 to a list of four values, each can be converted to different integers, and 228 matches the second integer 228.
Your subquery is returning a single string, not a list of values. If your oc_cart.option holds a single string, you can't use the IN( ) predicate in the way you're doing.
A workaround is this:
WHERE FIND_IN_SET(228, (SELECT option FROM oc_cart WHERE...))
But this is awkward. You really should not be storing strings of comma-separated numbers if you want to search for an individual number in the string. See my answer to Is storing a delimited list in a database column really that bad?

MySQL: compare a mixed field containing letters and numbers

I have a field in the mysql database that contains data like the following:
Q16
Q32
L16
Q4
L32
L64
Q64
Q8
L1
L4
Q1
And so forth. What I'm trying to do is pull out, let's say, all the values that start with Q which is easy:
field_name LIKE 'Q%'
But then I want to filter let's say all the values that have a number higher than 32. As a result I'm supposed to get only 'Q64', however, I also get Q4, Q8 and so for as I'm comparing them as strings so only 3 and the respective digit are compared and the numbers are in general taken as single digits, not as integers.
As this makes perfect sense, I'm struggling to find a solution on how to perform this operation without pulling all the data out of the database, stripping out the Qs and parsing it all to integers.
I did play around with the CAST operator, however, it only works if the value is stored as string AND it contains only digits. The parsing fails if there's another character in there..
Extract the number from the string and cast it to a number with *1 or cast
select * from your_table
where substring(field_name, 1, 1) = 'Q'
and substring(field_name, 2) * 1 > 32

mysql sort order, need slightly different result

I have a varchar column that I am currently sorting by using: ORDER BY (col_name+0)
This column contains both digits and non-digits, and the result of this sorting is this:
D3
D111
M123-M124
M136
4
9
10
25
37b
132
147-149
168b
168ca
This sorting is almost perfect for our application, but with one exception: we want the items that start with letters to display after those that start with numbers. This being the ideal result:
4
9
10
25
37b
132
147-149
168b
168ca
D3
D111
M123-M124
M136
I'm hoping this can be achieved in the select statement, rather than needing to loop through everything in code again after the select. Any ideas?
You can use this:
ORDER BY
col_name regexp "^[^0-9]",
case when col_name regexp "^[0-9]" then col_name + 0
else mid(col_name, 2, length(col_name )-1) + 0 end,
col_name
this will put rows that begins with a digit at the top. If col_name begins with a digit, I'm sorting by it's numeric value, if not I'm sorting by the numeric value of the string beginning at the second character.

Anomalous mysql behaviour on replace query

im using a v simple database and i have 3 columns A(bigINT 20) , B(bigInt 20) and c(DECIMAL(5,4)) , when i fire the following query i get the below mentioned results :
REPLACE INTO `my_table` SET `A` = 8,`B` = 44,`C` = 14;
i get these values in mysql A =8 , b= 44 and c as 9.9999 ! ?
any ideas as to why is this happening and what can i do to resolved this ?
DECIMAL(5,4) means that the number has at most 5 digits, 4 of them after decimal point. So 14 is simply overflow as it would require DECIMAL(6,4).
It must be cleared that 14 is overflow, because as constant precision point decimal it is internally 14.0000 here (so six digits over five).
So if you try to put 14.0000 (six digits) in DECIMAL(5,4) (five digits max) -> MySQL chooses value closest to the one you request. Therefore 14.0000 gets "rounded" to 9.9999.
To fit 14 in your column you can either extend it do DECIMAL(6,4) (to allow more digits in general) or change to DECIMAL(5,3) (which will allow one more digit before decimal point, but loses some precision of course).