Get rows that contain less than 4 digits only - mysql

I need to fetch the rows that contain less than 4 consecutive digits. I tried using this :
select mycolumn where mycolumn REGEXP '[0-9]{1,3}'
but this still returned rows such as
"Test Text 1234-1234"
I need to return these only:
Test 12
Test 234 Text
Test /2 Text
text 123 text 1234 text

Thanks to #BenM and #lad2025, the correct answer is:
'[[:<:]][0-9]{1,3}[[:>:]]'
http://sqlfiddle.com/#!9/af95e/3/0

Related

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

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

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.

Mysql find row with longest field value as beginning of a text

I have something like this in a mysql table.
ID Text
1 a
2 b
3 a-b
4 b-a
5 a-b-c
6 a-b-d
Usually you search a text inside a column, i just want the opposite, i want a query to search all columns inside a text. I dont know if it is possible.
if text is "a-b-f" It must return query with ID 3 ("a-b")
if text is "a-c" It must return query with ID 1 ("a")
if text is "b-a" It must return query with ID 2 ("b")
if text is "b-b" It must return query with ID 2 ("b")
if text is "a-b-c-d-e-f" It must return query with ID 5 ("a-b-c")
if text is "c-a-a" It must return an empty query
Thanks for the help.
PD: I was looking for something like this but all ways I found was search a text inside a column value and it is the opposite way as I said.
Could be using like
select ID from my_table
where 'your_text_seach' like concat ( text, '%')
order by length(text) desc
limit 1

Mysql searching multiple words using wildcard like and ordering by count of words matched

I have an innodb table 'not supporting fulltext' that i search using like statments
Select text,id from searchTable where text like '%sub1%' or text like '%sub2%' or text like '%sub3%'
group by text,
order by (CASE
when text LIKE 'sub1 %' then 1
when text LIKE 'sub1%' then 2
when text LIKE '% sub1%' then 3
when text LIKE '%sub1%' then 4
else 5
end),id
this returns results more or less as expected, yet i was wondering if i can also order it by the count of substrings that matched. for example order rows that has all 3 substrings first, followed by rows that matchs 2 out of 3 substrings, etc..
is it possible ? would it impact performance that much ?
my table contains around 50k rows, this current statement takes around 0.6ms to execute.
thanks
You can order by the number of matching substrings by doing:
order by ((test like '%sub1%') +
(text like '%sub2%') +
(text like '%sub3%')) desc
In an integer context, MySQL treats boolean values as integers, with 1 for true and 0 for false. So this counts the number of matches.

Mysql get values from column having more than certain characters without punctuation

How can I get all the values from mysql table field, having more than 10 characters without any special characters (space, line breaks, colons, etc.)
Let's say I have table name myTable and the field I want to get values from is myColumn.
myColumn
--------
1234
------
123 456
------
123:456
-------
1234
5678
--------
123-456
----------------
1234567890123
So here I would like to get all the field values except first one i.e. 1234
Any help is much appreciated.
Thanks
UPDATE:
Sorry if I was unable to give proper description of my problem. I have tried it again:
If there is count of more than 10 characters without punctuation, then retrieve that as well.
Retrieve all the values which have special characters like line break, spaces, etc.
Yes, I have primary key in this table if this helps.
The logic seems to be "more than 10 characters OR has special punctuation":
where length(mycol) > 10 or
mycol regexp '[^a-zA-Z0-9]'
SELECT MyColumn
From MyTable
WHERE MyColumn RLIKE '([a-z0-9].*){10}'
[a-z0-9] matches a normal character.
([a-z0-9].*) matches a normal character followed by anything.
{10} matches the preceding regexp 10 times.
The result is that this matches 10 normal characters with anything between them.