Properly select actual non-alphanumeric characters in MySQL column - mysql

I have a strange issue with my MySQL REGEXP SELECT query, I'm trying to select rows which contain non-alphanumeric characters. I've tried multiple things and it will match Cyrillic characters, but it does not consider apostrophes and spaces to be non-alphanumeric. Here are the queries I've tried:
SELECT * FROM `table` WHERE `name` REGEXP '^[^[:alnum:]]+$' LIMIT 0,10;
SELECT * FROM `table` WHERE `name` REGEXP '^[^a-z0-9]+$' LIMIT 0,10;
They both return nothing at all now because I've replaced all of the Cyrillic characters, except for spaces and apostrophes. I feel as though I must be doing something incorrectly, because it seems so illogical that MySQL would consider a space and apostrophe to be alphanumeric.

Have you tried:
SELECT * FROM table WHERE name REGEXP "[^a-z0-9]+" LIMIT 0,10;

Related

How to make Relative search in Mysql

In my database I have a record with title is "Học lập trình hướng đối tượng với Java"
When I search I use SELECT * FROM posts WHERE post_title like '%hoc%'
Or SELECT * FROM posts WHERE post_title like '%Học%'
But it doesn't work
How can I fix it?
The issue here is the character ọ in the database and the character ọ which you have used in the query are different even though they looks same.
The original character in the database is a combination of two characters, Latin small letter o (U+006F) and a Combining Dot Below ̣ (U+0323) character to form ọ. On the other hand, what you have used in the query is a single character, Latin Small Letter O With Dot Below ọ (U+1ECD).
You can fix the issue by using the same combination of characters as of the database:
SELECT * FROM posts WHERE post_title like '%Học%'
or you can use _ or % to skip unknown characters:
SELECT * FROM posts WHERE post_title like '%H__c%'
SELECT * FROM posts WHERE post_title like '%H%c%'
The like operator is case-sensitive, so if the case of the text you are searching for does not match the case of the text in the post_title column, the search will not return any results. You can use the LOWER() or UPPER() function to convert the text in the post_title column to lowercase or uppercase, respectively, before performing the search. For example:
select * from posts WHERE UPPER(post_title) like '%HỌC%';
OR
select * from posts WHERE LOWER(post_title) like '%học%';
OR
select * from posts WHERE post_title like '%Học%';
Working Demo: https://dbfiddle.uk/-fJrbLMk

MySQL - query to get all rows that a specific character is non-English

I have a table that has nvarchar elements.
This table has two kinds of elements:
elements with only digit characters
elements with digit characters and the 3rd character is non-English character
I want a query to get all rows that their 3rd character is non-English.
EDIT
use WHERE SUBSTRING(<table>.ColumnName, 3, 1) NOT BETWEEN '0' AND '9' worked for me either
I'd use regexp_like with a regex that the third character isn't a digit:
SELECT *
FROM mytable
WHERE REGEXP_LIKE(mycol, '..[^[:digit:]].*')
In MySQL versions older than 8.0, you could use the regexp operator:
SELECT *
FROM mytable
WHERE mycol REGEXP '..[^[:digit:]].*'
You can use RLIKE operator, below is the query for matching the third character which is not a digit and not an English alphabet
SELECT * FROM
mytable
where SUBSTR(mycol,3,1) NOT RLIKE '^[A-Za-z0-9]$';

Show/convert only alphanumeric data in sql query [duplicate]

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

mysql statement not to include an underscore in results

I have a mysql statement like this
$sql_insertwotd = "SELECT * FROM table WHERE word != '' AND word NOT LIKE '%\_%' ORDER BY RAND() LIMIT 1";
for some reason it it will still pull up a word with an underscore. I read this is the proper format to clarify an underscore with a .
I dont want it to select a word that contains an underscore
You have to use TWO BACKSLASHES (\\) and it should work
SELECT * FROM table WHERE word != '' AND word NOT LIKE '%\\_%'
ORDER BY RAND() LIMIT 1
You may need an explicit escape:
word NOT LIKE '%/_%' ESCAPE '/'
https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
By default \ is the escape character and your query should work as is.

MySQL Order By Symbol Preference

I was wondering if there was a way to make alphanumeric characters have a higher order of preference than symbols when doing an ORDER BY.
I.E. 'a' to come before '('
There are many ways round this, but I'd prefer the most elegant db approach.
This will give you the required output.
SELECT <column_name>
FROM <table_name>
ORDER BY CASE
WHEN <column_name> REGEXP '^[a-z]' OR <column_name> REGEXP '^[A-Z]' THEN CONCAT('1',<column_name>)
WHEN <column_name> REGEXP '^[0-9]' THEN CONCAT('2',<column_name>)
ELSE CONCAT(3, <column_name>)
END;
It will first take up the alphabets, then numbers and lastly other characters.