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}
Related
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.
I have table like this
I want get those record which content Unit Separator
I have try many things but not getting result.I try with char(31) and 0x1f and many other ways but not getting desired result.This is my query which i try
SELECT * FROM `submissions_answers` WHERE `question_id`=90 AND `answer` like '%0x1f%'
How can i do this? Please help me..
Problem
The expression you tried won't work because answer LIKE '%0x1f%' is looking for a string with literally '0x1f' as part of it - it doesn't get converted to an ASCII code.
Solutions
Some alternatives to this part of the expression that ought to work are:-
answer LIKE CONCAT('%', 0x1F, '%')
answer REGEXP 0x1F
INSTR(answer, 0x1F) > 0
Further consideration
If none of these work then there may be a further possibility. Are you sure the character seen in the strings is actually 0x1F? I only ask because the first thing I tried was to paste in ␟ but it turns out MySQL see this as a decimal character code of 226 rather than 31. Not sure which client you are using but if the 0x1F character is in the string, it might not actually appear in the output.
Demo
Some tests demonstrating the points above: SQL Fiddle demo
You can use:
SELECT * FROM submissions_answers WHERE question_id=90 AND instr(answer,char(31))>0
The keyword here being the INSTR MySQL function, which you can read about here. This function returns the position of the first occurrence of substring (char(31)) in the string (answer).
Yet another way...
SELECT * FROM `submissions_answers`
WHERE `question_id`=90
AND HEX(`answer`) REGEXP '^(..)*1F'
Explanation of the regexp:
^ - start matching at the beginning (of answer)
(..)* -- match any number (*) of 2-byte things (..)
then match 1F, the hex for US.
You could convert the answer column into a HEX value, and then look for values containing that hex string.
SELECT * FROM `submissions_answers`
WHERE HEX(`answer`) LIKE '%E2909F%'
I am using mySQL to query a field which would be LastName. I am looking for any errors in the field such as any special characters or numbers. I am not terribly familiar with SQL so this has been a challenge so far. I have written simple statements with REGEXP but I have run into some issues the REGEXP i was using was:
SELECT LastName FROM `DB`.`PLANNAME` where LastName REGEXP '^([0-9])'
now this turned up results where numbers were the first character in the string and i realized that if anything was in the middle of the string that started with a letter this would not pick it out.
To be clear i just need to find the errors not write a code to clean them out.
Any help would be greatly appreciated
Thanks
Pete
Something like this should do it for you.
SELECT column FROM table WHERE column REGEXP '[^A-Za-z]'
This will return any rows where a character that is not a-z. You might want to add in and '. For O'briens and von lansing etc. Any characters you think are acceptable should go in the character class [], http://www.regular-expressions.info/charclass.html.
Demo: https://regex101.com/r/nC9cG7/1
Maybe you are looking for something like this:
SELECT LastName FROM `DB`.`PLANNAME` WHERE NOT LastName REGEXP '[A-Za-z0-9]';
Here is a documentation on this:
Table 12.9 String Regular Expression Operators
I need a MySQL pattern to match a number, followed by a question mark.
I need something like
... like '%[0-9]?%'
but I have no idea how to create this regular expression.
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html does not help.
Thanks!
you could try this:
SELECT * FROM YourTable WHERE YourField REGEXP '[0-9]\\?'
That will return rows where YourField contains a number followed by a ? anywhere in the value.
If you want it to only match if the whole field is a number followed by a ?. I.e. 9? then you could use this regex instead:
^[0-9]\\?$
I guess you're looking for something like this:
select * from table
where field rlike '[0-9]\\?'
Remember to escape the question mark. Otherwise, it will make the number optional.
Source.
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.