I'm trying to find '%%' string in content.
SELECT * FROM myTable WHERE myColumn LIKE '%\%\%%'
But it returns also the rows, where only one percent sign found.
How to find only the rows, where two percent signs exists?
Thanks.
you can also use REGEXP for this,
SELECT *
FROM table1
WHERE str REGEXP '%%'
SQLFiddle Demo
If you are indeed only looking for literal % and don't need the wildcards, this would be made simpler using LOCATE(), where the % will be parsed as literals requiring no escaping.
SELECT * FROM myTable WHERE LOCATE('%%', myColumn) > 0;
Related
I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';
I am trying to find all values that do not start with a number
I have tried this query, but not sure if the REGEXP is correct. This seem to be returning any value the does not contains a number
SELECT * FROM table where address NOT REGEXP '[0-9]'
I think this fixed the issue
SELECT * FROM table where address NOT REGEXP '^[0-9]'
In MySQL if you try to convert a string to a number then it will start from the beginning taking the digits and convert as much as possible.
If a string does not start with a number then the result is 0.
select * from your_table
where address * 1 = 0
SQLFiddle demo
You can use explicit conversion with cast(str_column as unsigned) or implicit conversion by using a mathematical operator like * 1
I have the following strings in the following pattern in a table in my db:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I am trying to match all strings that start with a specific pattern split by underscores i.e.this_is_my_string_ and then a wildcard final section
Unfortunately there is an added complication where some strings like the following:
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
Therefore taking the following as examples:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
I am trying to have returned:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I have no idea how to do this with a like statement. Is this possible if so how?
EDIT
There is one final complication:
this_is_my_string
needs to be supplied as a list i.e in
(this_is_my_string, this_is_my_amazing_string, this_is_another_amazing_string)
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
It might be faster if you have an index on afield and do
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
AND afield LIKE 'this_is_my_string_%'
After edit of question:
Either
SELECT * FROM atable
WHERE afield REGEXP '(this_is_my_string|this_is_my_amazing_string)_[a-z]+'
or maybe you want something like having a table with the prefixes:
SELECT *
FROM atable AS t,
prefixes AS p
WHERE afield REGEXP CONCAT(p.prefix, '_[a-z]+')
As by the reference documentation this should not be possible, as a pattern (string literal) is required. Give it a try nevertheless.
There the answer of #KayNelson with LIKE (?) and INSTR might do instead of REGEXP.
try this
SELECT * FROM db.table WHERE strings LIKE 'this_is_my_string_%' AND instr(Replace(strings,"this_is_my_string_",""),"_") = 0;
It checks if more _ occurs after replacing the standard "this_is_my_string_"
If someone passes a '%' to a field that compares in my sql with su.username LIKE CONCAT('%', email ,'%')) it returns all rows. It ends up looking like su.username LIKE CONCAT('%%%'). Can I get around this in anyway without filtering out the '%'?
I'm assuming you mean you want to escape the % so it matches a literal % instead of anything.
In that case, you just need:
... su.username LIKE CONCAT('%',REPLACE(email,'%','\\%'),'%')
You need to escape the %, so it literally matches '%'
select * from mytable
where mycol like '%\%%';