MySQL REXEXP doesn't work propertly - mysql

SELECT 'pharase' REGEXP '[^a-zA-Z]+' - it verifies that the phrase does not contain English chars (or not?).
Why is query SELECT '123g' REGEXP '[^a-zA-Z]+' -> 1 show 1 instead 0?

To check if a string only contains English chars, use
SELECT 'pharase' REGEXP '^[a-zA-Z]+$'
where:
^ - asserts the position at the start of the string
[a-zA-Z]+ - 1 or more ASCII letters
$ - the end of string
SELECT '123g' REGEXP '[^a-zA-Z]+' -> 1 shows 1 because the string contains 123, non-letters, a partial match is found with REGEXP (unlike LIKE that requires a full string match).

Related

How to search by specific pattern in MySQL?

I have below pattern of data in field
XXX-XX-XXX
Some of the data don't have that patterns.
So need to search those records.
SELECT * FROM table WHERE `name` NOT REGEXP '^.{10}$'
SELECT * FROM table WHERE `name` NOT REGEXP '^..........$'
Above 2 queries works fine. But not 100%.
Can I filter by {3}-{2}-{3} ?
You want to match a string with 3 alphanumeric chars followed with -, followed with 2 alphanumeric and then again a hyphen and 3 last alphanumeric in the string.
Use
'^[[:alnum:]]{3}-[[:alnum:]]{2}-[[:alnum:]]{3}$'
Details
^ - start of the string
[[:alnum:]]{3} - 3 alphanumeric chars
- - a hyphen
[[:alnum:]]{2}- - 2 alphanumeric chars and a -
[[:alnum:]]{3} - 3 alphanumeric chars
$ - end of string.
See MySQL "Regular Expression Syntax":
Character Class Name Meaning
alnum Alphanumeric characters

mysql where string ends with numbers

My table column contains values like:
id | item
-------------
1 | aaaa11a112
2 | aa1112aa2a
3 | aa11aa1a11
4 | aaa2a222aa
I want to select only rows where value of item ends with numbers.
Is there something like this?
select * from table where item like '%number'
You can use REGEXP and character class
select * from table where item REGEXP '[[:digit:]]$'
DEMO
Explanation:
[[:digit:]] >> Match digit characters
$ >> Match at the end of the string
Within a bracket expression (written using [ and ]), [:character_class:] represents a character class that matches all characters belonging to that class.
SIDENOTE:
Other helpful mysql character classes to use with REGEXP, taken from the documentation:
Character Class Name Meaning
alnum Alphanumeric characters
alpha Alphabetic characters
blank Whitespace characters
cntrl Control characters
digit Digit characters
graph Graphic characters
lower Lowercase alphabetic characters
print Graphic or space characters
punct Punctuation characters
space Space, tab, newline, and carriage return
upper Uppercase alphabetic characters
xdigit Hexadecimal digit characters
you can use REGEXP
select * from table where RIGHT(item ,1) REGEXP '^-?[0-9]+$';
Yes you can use like for numbers.
select * from table where item like '%1'
This will work

How do I get first 1 or 2 characters of a string until a number appears in MYSQL?

I have a postcode field and want to only get the first letters that appear before a number appears. e.g. E11 would return E, HD4, would return HD.
Use regex to find the position of the first number
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
and then use substring to get from 0 to this number
http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php
I hope this helps . . .
SELECT IF (postcode REGEXP "^[A-Z][A-Z]",LEFT(postcode,2),LEFT(postcode,1));
These examples demonstrate:
SELECT IF ("H5 7PL" REGEXP "^[A-Z][A-Z]",LEFT("H5 7PL",2),LEFT("H5 7PL",1));
-> "H"
SELECT IF ("HD5 7PL" REGEXP "^[A-Z][A-Z]",LEFT("HD5 7PL",2),LEFT("HD5 7PL",1));
-> "HD"

How to select rows with a certain number in one of its columns?

In one of the columns of my table (say it's called foo), it stores a set of 10 numbers, e.g:
1, 5, 8, 3, 4, 6, 9, 7, 12, 15
I wish to select all rows of my table which have the number 6 in the foo column.
Well, you might say, this sounds simple enough, just do a string search on the column. But the problem is, then you would also select rows with 16 in them, as 16 contains a 6.
I also thought about searching for , 6, instead, but I realized if 6 was on the very end or front of the string, it won't match.
How can I overcome this problem?
use MySQL built-in function called FIND_IN_SET.
SELECT *
FROM tablename
WHERE FIND_IN_SET('6', REPLACE(foo,' ','')) > 0
SQLFiddle Demo
FIND_IN_SET()
From Docs:
Returns a value in the range of 1 to N if the string str is in the
string list strlist consisting of N substrings. A string list is a
string composed of substrings separated by “,” characters. If the
first argument is a constant string and the second is a column of type
SET, the FIND_IN_SET() function is optimized to use bit arithmetic.
Returns 0 if str is not in strlist or if strlist is the empty string.
Returns NULL if either argument is NULL. This function does not work
properly if the first argument contains a comma (“,”) character.
You can append a ', ' to the beginning and end of foo and search:
SELECT *
FROM bar
WHERE ', ' + foo + ', ' LIKE '%, 6, %'

How to: Select from MYSQL text field type only the numbers that start with 89 using REGEXP?

I have tried in many ways to select from text fields only the numbers that start with 89. I don't have a fix length after the first 2 numbers.
How can I do this to work properly and not get numbers like 389xxxxxx in results, for example. THE minimum length should be at least 8 characters.
Thank you!
If your column is integer, then you can probably do something like:
select * from my_table where cast(int_column) as char) like '89______%'
(that's 6 underscore characters before the percentage char)
If it's character value, then you can do this:
SELECT * FROM mytable WHERE char_column REGEXP "^89[[:digit:]]{6,}$"
If your column is numeric with decimal places and you want only integer values, then you need to do something like
SELECT * FROM mytable WHERE cast(numeric_column as char) REGEXP "^89[[:digit:]]{6,}$"
Edit: It seems Tim has edited his answer, which I referred to, so I edited my answer to include his code for column of character type.
SELECT * FROM foo WHERE bar REGEXP "([^0-9]89[0-9]*)|(^89[0-9]*)"
SELECT * FROM mytable WHERE mycolumn REGEXP "(^|[^[:digit:]])89[[:digit:]]{6,}";
will do this:
( # Either match
^ # the start of the string
| # or
[^[:digit:]] # any character except a digit
) # End of alternation.
89 # Match 89
[[:digit:]]{6,} # plus at least 6 more digits.