Consider a table datatbl like this:
+----------+
| strfield |
+----------+
| abcde |
| fgHIJ |
| KLmno |
+----------+
I want to write a query something like this:
select * from datatbl where strfield rlike '[a-z]*';
As in a non-SQL regex, I'd like to return the lowercase row with abcde, but not the rows with capitals. I cannot seem to find an easy way to do this. Am I missing something stupid?
The MySQL REGEXP/RLIKE sucks for this - you need to cast the data as BINARY for case sensitive searching:
SELECT *
FROM datatbl
WHERE CAST(strfield AS BINARY) rlike '[a-z]*';
You'll find this raised in the comments for the REGEXP/RLIKE documentation.
Edit: I've misread OP and this is solution for the opposite case where MySQL is in SENSITIVE collation and you need to compare string in INSENSITIVE way.
MySQL 5.x
You can workaround it using LOWER() function, too.
SELECT *
FROM datatbl
WHERE LOWER(strfield) RLIKE '[a-z]*';
MySQL 8+
If you are running MySQL 8+, you can also use case-insensitive switch in REGEXP_LIKE() function.
SELECT *
FROM datatbl
WHERE REGEXP_LIKE(strfield, '[a-z]*', 'i');
For case-sensitive regex you can use REGEXP_LIKE() with match type c like this:
SELECT * FROM `table` WHERE REGEXP_LIKE(`column`, 'value', 'c');
Related
have that little sql select:
select * from import_daten WHERE lastname REGEXP 'dipl\.|dr\.';
And just want to filter the rows with ing. and dipl. but with that statement i also get the people wtih for e.g. "Abendroth" in Lastname. Because the "dr" in Name.
Same is with
select * from import_daten WHERE lastname REGEXP 'dipl.|dr.';
How is it possible to include the full-stop correct within the regexp?
REGEXP '(dipl|dr)[.]'
Be careful of start/end of word:
mysql> SELECT 'dr.' REGEXP 'dr[.][[:>:]]', 'dr.' REGEXP 'dr[.]';
+-----------------------------+----------------------+
| 'dr.' REGEXP 'dr[.][[:>:]]' | 'dr.' REGEXP 'dr[.]' |
+-----------------------------+----------------------+
| 0 | 1 |
+-----------------------------+----------------------+
Notice how it fails? That is because . is not a character that can exist in a 'word'.
Also, I used [.] instead of \. because of the problem of escaping the escape character -- in some situations you need \\.; in others you might need \\\\.. Too confusing.
If necessary you can use 'word start': REGEXP '[[:<:]](dipl|dr)[.]'
I think you want this
select * from import_daten WHERE lastname REGEXP '(dipl\.)|(ing\.)';
You probably want to make sure the pattern is at a "word boundary." MySQL's regular expression syntax has special character sequences for that:
select * from import_daten WHERE lastname REGEXP '[[:<:]](dipl\.|dr\.)[[:>:]]';
See http://dev.mysql.com/doc/refman/5.7/en/regexp.html. It's nearly the last item on the page before that page's user comments.
Hi I am writing my own MySQL query where I need a result of records as follows.
Word in a table - ABC XYZ
My string - ABC XYZQWER
when I ran my query as below -
SELECT * FROM myTABLE where `column` LIKE 'ABC XYZQWER%';
I am getting empty result. I am aware of the fact that MySQL LIKE matches the result of string.
I need a way to figure this out.
I I searched it using 'ABC X' - it is giving me a proper result.
You can use the function LOCATE():
SELECT `column`
FROM myTable
WHERE LOCATE(`column`, 'ABC XYZQWER') = 1;
As long as there is a value ABC XYZ in the column named column, the result of the query will be at least:
+---------+
| column |
+---------+
| ABC XYZ |
+---------+
Finding an inner match
Finding a matching string like 'BC', which is inside the search string 'ABC XYZQWER', is possible by using the compare operator >=. So the WHERE clause will look like this:
WHERE LOCATE(`column`, 'ABC XYZQWER') >= 1;
It is because you dont have a work which has QWER. You are actually searching for a word which is not present. So you are getting a zero result.
For eg:
Word : qwertyuiuioo
search String : qwerty
select * from table where word like qwerty% you will get the result.
% takes any number of characters after the letters you have given which is not matching any value in the table.
Try this:
SELECT * FROM myTABLE a WHERE 'ABC XYZQWER' LIKE CONCAT(a.column, '%');
Here are some examples of how one might use LIKE clause in SQL queries:
SELECT * FROM myTABLE where column LIKE 'ABC%';// matches ABCD, ABC D but not DABC
SELECT * FROM myTABLE where column LIKE '%ABC%';// matches any string that contains ABC anywhere in the string eg. DABC, D ABC but not D AB C
for your case you would do something like this:
SELECT * FROM myTABLE where column LIKE 'ABC XYZ%';
You won't be able to do perfect substring searches although you can apply Levenshtein distance searches as described here (Levenshtein Distance MySQL Function). But do note these work a bit differently from LIKE clause in a way that it gives you the result based on the distance you specify for a search.
And after that you can use it like this:
SELECT * FROM mytable WHERE levenshtein("ABC XYZQWER",column) <= 4
This will give you the result set you are looking for; it will also give other words which fall under this range.
If I have a table with a column called TITLE which contains text of mixed case, e.g.
Vinashin to Receive Government Loans to Pay Workers
German government concerned over rise in inflation
Is it possible to perform an SQL LIKE query such as:
SELECT * FROM MYTABLE WHERE TITLE LIKE '%Government%'
But which would only return the first row and not the second?
MYSQL's LIKE seems to ignore case.
From the documentation:
The following two statements illustrate that string comparisons are not case sensitive unless one of the operands is a binary string:
mysql> SELECT 'abc' LIKE 'ABC';
-> 1
mysql> SELECT 'abc' LIKE BINARY 'ABC';
-> 0
So you can use LIKE BINARY '%Government%' to make a case-sensitive comparison.
You can use:
LIKE BINARY
instead of LIKE and it will match case sensitive.
I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.
I am trying to use the LIKE operator in my query as follows:
mysql> select cat_title from category where cat_title like '%Indian%Restaurant%';
+--------------------+
| cat_title |
+--------------------+
| Indian_Restaurants |
+--------------------+
1 row in set (2.59 sec)
However, since I want to do a case insensitive search, I am trying:
mysql> select cat_title from category where UPPER(cat_title) like UPPER('%Indian%Restaurant%');
Empty set (2.83 sec)
Why is the second query not working?
Most likely the collation on the cat_title column is case insensitive. Use
... cat_title LIKE '%Indian%Restaurant%' COLLATE utf8_bin
See also
How can I make SQL case sensitive string comparison on MySQL?
Case Sensitive collation in MySQL
Not sure what's your question. The UPPER function won't work before a 'like' operator. If you want a case-insensitive search, use the operator 'like' only, since it's case-insensitive.
On the other hand, if you want a case-sensitive search, you may try using
cat_title COLLATE Latin1_General_BIN LIKE '%Indian%Restaurant%'