SELECT only rows that contain only alphanumeric characters in MySQL
up vote
38
down vote
favorite
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.
mysql regex
add a comment |
up vote
38
down vote
favorite
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.
mysql regex
add a comment |
up vote
38
down vote
favorite
up vote
38
down vote
favorite
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.
mysql regex
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.
mysql regex
mysql regex
asked Sep 24 '09 at 12:57
code_burgar
9,36742751
9,36742751
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
69
down vote
accepted
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
@Md.MahabuburRahman: All a blank between9
and]
:'^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared toisnumeric()
Use specialized functions when you can.
– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
add a comment |
up vote
7
down vote
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:]]+$';
add a comment |
up vote
3
down vote
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
-1
down vote
Try this
select count(*) from table where cast(col as double) is null;
How's this going to work ifcol
is'ABC'
?
– ebyrob
Nov 9 '16 at 16:08
add a comment |
up vote
-6
down vote
Change the REGEXP
to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1471523%2fselect-only-rows-that-contain-only-alphanumeric-characters-in-mysql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
69
down vote
accepted
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
@Md.MahabuburRahman: All a blank between9
and]
:'^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared toisnumeric()
Use specialized functions when you can.
– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
add a comment |
up vote
69
down vote
accepted
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
@Md.MahabuburRahman: All a blank between9
and]
:'^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared toisnumeric()
Use specialized functions when you can.
– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
add a comment |
up vote
69
down vote
accepted
up vote
69
down vote
accepted
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
edited Feb 4 '14 at 9:47
Jess Stone
602819
602819
answered Sep 24 '09 at 12:59
Aaron Digulla
244k83463683
244k83463683
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
@Md.MahabuburRahman: All a blank between9
and]
:'^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared toisnumeric()
Use specialized functions when you can.
– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
add a comment |
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
@Md.MahabuburRahman: All a blank between9
and]
:'^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared toisnumeric()
Use specialized functions when you can.
– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
Okey it done properly, but it does not show space including data field. How do i get space including data field by this query.
– Md. Mahabubur Rahman
Jul 27 '13 at 7:14
3
3
@Md.MahabuburRahman: All a blank between
9
and ]
: '^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
@Md.MahabuburRahman: All a blank between
9
and ]
: '^[A-Za-z0-9 ]+$'
– Aaron Digulla
Jul 29 '13 at 7:40
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
is regexp efficient? I remember doing this with isnumeric on the last character. this is interesting. thanks select name from birt where isnumeric(RIGHT(name,1)) =1
– chungtinhlakho
Jun 3 '16 at 1:18
@chungtinhlakho No, regexp is very inefficient compared to
isnumeric()
Use specialized functions when you can.– Aaron Digulla
Jun 9 '16 at 14:05
@chungtinhlakho No, regexp is very inefficient compared to
isnumeric()
Use specialized functions when you can.– Aaron Digulla
Jun 9 '16 at 14:05
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
thanks for the tip. :)
– chungtinhlakho
Jun 9 '16 at 22:59
add a comment |
up vote
7
down vote
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:]]+$';
add a comment |
up vote
7
down vote
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:]]+$';
add a comment |
up vote
7
down vote
up vote
7
down vote
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:]]+$';
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:]]+$';
answered Sep 24 '09 at 13:03
John Kugelman
239k51397451
239k51397451
add a comment |
add a comment |
up vote
3
down vote
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
add a comment |
up vote
3
down vote
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
add a comment |
up vote
3
down vote
up vote
3
down vote
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
answered Sep 24 '09 at 13:01
Yannick Motton
24.1k43352
24.1k43352
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Mar 3 '16 at 9:46
answered Mar 2 '16 at 15:01
apollo
30929
30929
add a comment |
add a comment |
up vote
-1
down vote
Try this
select count(*) from table where cast(col as double) is null;
How's this going to work ifcol
is'ABC'
?
– ebyrob
Nov 9 '16 at 16:08
add a comment |
up vote
-1
down vote
Try this
select count(*) from table where cast(col as double) is null;
How's this going to work ifcol
is'ABC'
?
– ebyrob
Nov 9 '16 at 16:08
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Try this
select count(*) from table where cast(col as double) is null;
Try this
select count(*) from table where cast(col as double) is null;
edited Oct 3 '16 at 15:33
Andy♦
30.2k21102158
30.2k21102158
answered Oct 3 '16 at 15:29
Akshay Singh
256
256
How's this going to work ifcol
is'ABC'
?
– ebyrob
Nov 9 '16 at 16:08
add a comment |
How's this going to work ifcol
is'ABC'
?
– ebyrob
Nov 9 '16 at 16:08
How's this going to work if
col
is 'ABC'
?– ebyrob
Nov 9 '16 at 16:08
How's this going to work if
col
is 'ABC'
?– ebyrob
Nov 9 '16 at 16:08
add a comment |
up vote
-6
down vote
Change the REGEXP
to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
add a comment |
up vote
-6
down vote
Change the REGEXP
to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
add a comment |
up vote
-6
down vote
up vote
-6
down vote
Change the REGEXP
to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
Change the REGEXP
to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
edited Aug 2 '12 at 18:41
GDP
6,39143467
6,39143467
answered Mar 12 '12 at 11:23
INTERESTING FACTS
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1471523%2fselect-only-rows-that-contain-only-alphanumeric-characters-in-mysql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown