Mysql searching for string in text - mysql

I can't help but think my syntax is wrong.
SELECT * FROM `rework` WHERE rw_pd LIKE 'FIB'
The SQL executes with a result of 0 rows. I KNOW there are rows with that string in that field. Anybody see any stupid mistakes?

You have to specify the wildcard if your have strings which contains FIB chained to other text:
SELECT * FROM `rework` WHERE rw_pd LIKE '%FIB%'
Check also this link for various wildcards usage.

Have you tried adding %, which is a wildcard.
SELECT * FROM rework WHERE rw_pd LIKE '%FIB%'

Related

Regex to find exact match in a string works elsewhere but not on MySql Query

The given string is a comma separated value, for example the string below
29,30,31,32,33,34,35,36,192,225,228,233,239,240,144,145
I want to find the exact whole match of any number in the string, and I did write a regex to find it. When I checked it on regex101, it worked well. But when I wrote the same regex on MySql Query, it did not work
(SELECT * FROM table_name WHERE (value REGEXP '.*\b22\b,{0,1}.*')
I did find another query that works, it used concat() function.
SELECT * FROM table_name WHERE CONCAT(',',value,',') like '%,22,%'
Edit: The answer from #anubhava also works (which can be found on the very first comment on this question). Since I am able to pick only one answer as Solved, I just wanted to let others know.
Thanks again
On your version of MySQL, most likely word boundaries are denoted by [[:<:]] and [[:>:]]:
SELECT *
FROM table_name
WHERE value REGEXP '[[:<:]]22[[:>:]]';
I believe that starting with MySQL 8+, it is possible to use the more standard \b to represent a word boundary. Check the demo link below to see it working on MySQL 5.7.
Demo
There is a function just for this task:
WHERE FIND_IN_SET('22', '29,30,31,32,33,34,35,36,192,225,228,233,239,240,144,145')

MYSQL Find entries that contain more than 7 numbers

I need to find entries that contain more than 7 numbers in one of my mysql tables BUT the numbers are separated by letters or anything else.
What I have is this little piece of code I use to find entries like dsc123456789:
select * from crawl where title regexp '[0-9]{7}'
How can I find entries like dsc-123-456_78B9? I tried different things but without success so far.
Thanks
You can use the following solution:
SELECT *
FROM crawl
WHERE title REGEXP '(([^[:digit:]])?[[:digit:]]){8,}';
Why the original query of the answer doesn't work?
-- this query doesn't work!
SELECT *
FROM crawl
WHERE title REGEXP '\d([^\d]?\d){7,}'
MySQL can't use character groups like \d (digits). So the query fails every time. On PHP and other languages the regular expression would look like this:
\d([^\d]?\d){7,}
but on MySQL this isn't valid. So you have to use the character classes of MySQL to solve this:
(([^[:digit:]])?[[:digit:]]){8,}
Hint: Make sure you use {8} or {8,} instead of {7} since you want to find all entries with more than 7 numbers / digits.

How to make this regex in mysql?

A few min ago I found out that mysql accepts regex, and is great becouse I think it can solve my problem, but I don't know how to write it. So, I need something like this:
SELECT name FROM products WHERE name REGEXP 'regex code'
To give a little more explanation, the name must be in this format: 123425HT and not string99-123425HT. The 123425HT and string99-123425HT is taken arbitrary
Please Help, thanks!
Something like that:
SELECT * FROM t2 WHERE str REGEXP "^([0-9]+)([a-zA-Z]{2})$";
This regexp will found strings which starting from any digits and two small or big characters, for example:
123123hd
12345435MF
6572Sg
If you want use only 6 digits change from [0-9]+ to [0-9]{6}

Looking to extract data between parentheses in a string via MYSQL

Can someone please help. I have been searching and encountered/modified this code I am getting a 1 or 0 as a result. 1 if there is something between () and 0 if there is not. I am looking to find exactly what is between them not if there is something. So if I have a string in afield that looks like this: "ABC (989) Hello" currently I get 1 as my result I would like to get "989". Any help would be greatly appreciated.
select , OUTCNTCTNOTE regexp '[(]|\\[)]' as test
from trcalls.callcoding;
To complete the first answer, because the third parameter passed to substr is the length of the substring, we need to subtract the index of the opening parantheses, so:
substr(columnname,instr(columnname,"(") + 1, instr(columnname,")") - instr(columnname,"(") - 1)
should do the trick
select substr(columnname,instr(columnname,"(") + 1, instr(columnname,")")) as temp from mytable
something close, I tested this. Please see if this helps!
Mysql's regexes don't support capturing or replacing. They're purely for matching. You'd need to use regular string operations to do the actual extraction:
SELECT ...string stuff here...
FROM yourtable
WHERE OUTCNTCTNOTE regexp ....
If your strings are fairly 'regular' and you don't have to worry about multiple sets of brackets in any field, then using LOCATE() and SUBSTR() would do the trick.

mysql string contained within a search string

What I need seems simple, but I haven't been able to pull it off so far. Maybe it's just these late hours, or maybe it's not that simple after all, I don't know any more :)
So, here's the thing. I want to be able to check whether the search string from my site contains any of the fields from a particular column in my database. So, it would be the opposite of the usual one:
mysql_query("
SELECT *
FROM `table`
WHERE `column` LIKE '%{$search}%'
");
which looks for the fields with values where the search string is contained.
What would be the easiest way, using some regular expressions or...?
Thanks a bunch!
Just do it the other way around
SELECT *
FROM `table`
WHERE '{$search}' LIKE concat('%', `column`, '%')
bear with me for the proper syntax for variable escaping for SQL-injection.
your query is also good as you want.
but still you can try this.
You can search your string by using MATCH() AGAINST() statement in mysql.
mysql_query("SELECT * FROM table_name WHERE MATCH(field11,field12) AGAINST ('searchstring')");
Here your field must be having Fulltext datatype.
This may work good.
Thanks.