Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT only rows that contain only alphanumeric characters in MySQL

Tags:

regex

mysql

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.

like image 911
code_burgar Avatar asked Sep 24 '09 12:09

code_burgar


People also ask

How do I select alphanumeric values in SQL?

Using LIKE clause Val LIKE '%[A-Z]%', it ensures that string should contain alphanumeric characters. Val LIKE '%[0-9]%', it ensures that string should contain numeric characters. Lets see the output of T-SQL, you can see it returns only alphanumeric string only.

Which datatype is used for alphanumeric values in mysql?

You can use these SQL data types to store alphanumeric data: CHAR and NCHAR data types store fixed-length character literals. VARCHAR2 and NVARCHAR2 data types store variable-length character literals. NCHAR and NVARCHAR2 data types store Unicode character data only.

Is Char alphanumeric in mysql?

CHAR. Let's start with the simplest alphanumeric data type: CHAR (character, or character). This type of data allows you to store short texts of up to 255 characters in length in characters that we define, even if we do not use it.


1 Answers

Try this code:

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

This makes sure that all characters match.

like image 82
Aaron Digulla Avatar answered Oct 06 '22 12:10

Aaron Digulla