SELECT only rows that contain only alphanumeric characters in MySQL









up vote
38
down vote

favorite
10












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.










share|improve this question

























    up vote
    38
    down vote

    favorite
    10












    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.










    share|improve this question























      up vote
      38
      down vote

      favorite
      10









      up vote
      38
      down vote

      favorite
      10






      10





      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 24 '09 at 12:57









      code_burgar

      9,36742751




      9,36742751






















          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.






          share|improve this answer






















          • 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 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










          • @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

















          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:]]+$';





          share|improve this answer



























            up vote
            3
            down vote













            Try this:



            REGEXP '^[a-z0-9]+$'


            As regexp is not case sensitive except for binary fields.






            share|improve this answer



























              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.






              share|improve this answer





























                up vote
                -1
                down vote













                Try this



                select count(*) from table where cast(col as double) is null;





                share|improve this answer






















                • How's this going to work if col is 'ABC'?
                  – ebyrob
                  Nov 9 '16 at 16:08

















                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






                share|improve this answer






















                  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
                  );



                  );













                  draft saved

                  draft discarded


















                  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.






                  share|improve this answer






















                  • 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 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










                  • @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














                  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.






                  share|improve this answer






















                  • 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 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










                  • @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












                  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.






                  share|improve this answer














                  Try this code:



                  SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'


                  This makes sure that all characters match.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  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 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










                  • @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
















                  • 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 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










                  • @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















                  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












                  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:]]+$';





                  share|improve this answer
























                    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:]]+$';





                    share|improve this answer






















                      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:]]+$';





                      share|improve this answer












                      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:]]+$';






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 24 '09 at 13:03









                      John Kugelman

                      239k51397451




                      239k51397451




















                          up vote
                          3
                          down vote













                          Try this:



                          REGEXP '^[a-z0-9]+$'


                          As regexp is not case sensitive except for binary fields.






                          share|improve this answer
























                            up vote
                            3
                            down vote













                            Try this:



                            REGEXP '^[a-z0-9]+$'


                            As regexp is not case sensitive except for binary fields.






                            share|improve this answer






















                              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.






                              share|improve this answer












                              Try this:



                              REGEXP '^[a-z0-9]+$'


                              As regexp is not case sensitive except for binary fields.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 24 '09 at 13:01









                              Yannick Motton

                              24.1k43352




                              24.1k43352




















                                  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.






                                  share|improve this answer


























                                    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.






                                    share|improve this answer
























                                      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.






                                      share|improve this answer














                                      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.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Mar 3 '16 at 9:46

























                                      answered Mar 2 '16 at 15:01









                                      apollo

                                      30929




                                      30929




















                                          up vote
                                          -1
                                          down vote













                                          Try this



                                          select count(*) from table where cast(col as double) is null;





                                          share|improve this answer






















                                          • How's this going to work if col is 'ABC'?
                                            – ebyrob
                                            Nov 9 '16 at 16:08














                                          up vote
                                          -1
                                          down vote













                                          Try this



                                          select count(*) from table where cast(col as double) is null;





                                          share|improve this answer






















                                          • How's this going to work if col is 'ABC'?
                                            – ebyrob
                                            Nov 9 '16 at 16:08












                                          up vote
                                          -1
                                          down vote










                                          up vote
                                          -1
                                          down vote









                                          Try this



                                          select count(*) from table where cast(col as double) is null;





                                          share|improve this answer














                                          Try this



                                          select count(*) from table where cast(col as double) is null;






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          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 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















                                          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










                                          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






                                          share|improve this answer


























                                            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






                                            share|improve this answer
























                                              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






                                              share|improve this answer














                                              Change the REGEXP to Like



                                              SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'


                                              this one works fine







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Aug 2 '12 at 18:41









                                              GDP

                                              6,39143467




                                              6,39143467










                                              answered Mar 12 '12 at 11:23









                                              INTERESTING FACTS

                                              1




                                              1



























                                                  draft saved

                                                  draft discarded
















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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







                                                  Popular posts from this blog

                                                  𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                  Crossroads (UK TV series)

                                                  ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế