I have in table column pnum_s
I need get only that rows, which value in column pnum_s is exactly 10 symbol and all these symbols are only digits
what query must write for this?
I am trying
SELECT * FROM mytable WHERE pnum_s REGEXP '^\d+$'
But this not returns 0 rows
The pattern you are looking for is either '^[0-9]{10}$' or '^[[:digit:]]{10}$'.
Everything is in the manual.
I think with Mysql you'll want something like this:
^[[:digit:]]{10}$
Check out the reference page.
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
What you're looking for is this:
SELECT * FROM mytable WHERE pnum_s REGEXP '^[[:digit:]]{10}$';
try this pattern:
'^[0-9]{10}$'
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 '[\\.]';
basically, i need a query that looks mostly like this...
SELECT column FROM tablename WHERE column LIKE '%images/uploads%';
There only catch here is, that I only want a return where anything after uploads is ok as long as it's NOT a forward slash /
So, images/uploads/somethingelse is NOT what i'm looking for
images/uploadssomethingelse IS what i'm looking for.
Any ideas?
Thanks!
This would help you
SELECT column FROM tablename WHERE column LIKE '%images/uploads%' AND column NOT LIKE '%images/uploads/%';`
SELECT column
FROM tablename
WHERE
column LIKE '%images/uploads%'
and column not like '%images/uploads%/%';
SELECT column FROM tablename WHERE column REGEXP 'images/uploads[^/]*$';
I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
I have a column that contains values like 5A898, 89KAS, 89ASD.
I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?
So...
SELECT column
FROM table
WHERE column = ?
WHERE column LIKE '__A%'
^^-- 2 underscores
should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.
Maybe you can use MySQL's SUBSTRING
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'
where right(left(col,3),1) = 'A'
That can help... MarcB's answer is cleaner
You can do this with a regex, but I think it might be easier just coping with a string operation here:
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A';