Ignore apostrophes, commas, hyphens, etc in a search - mysql

Example of the problem:
SELECT * FROM `table`
WHERE `content`
RLIKE "[[:<:]]didnt[[:>:]]"
The problem is, when I search for things like "didnt", columns with "didn't" (with an apostrophe) arent found. And if I were to search for "didn't", columns with "didnt" (without an apostrophe) arent found.
What change do I need to make so that it ignores apostrophes, commas, hyphens, etc, and gives results regardless of if there are/arent apostrophes.

why don't you try with an |options
SELECT * FROM table
WHERE
(content RLIKE "[[:<:]]didnt[[:>:]]|[[:<:]]didn't[[:>:]]|[[:<:]]didn..t[[:>:]]|ETC)"
source: http://www.sqlines.com/mysql/regexp_rlike

You could use nested replace functions:
SELECT *
FROM `table`
WHERE
replace(replace(replace(`content`, '\'', ''), ',', ''), '-', '')
RLIKE "[[:<:]]didnt[[:>:]]"

Related

how to select based on comma separated values in column

In my table, one of the column has comma separated values. I would like to select the entry based on one or two values if found in that column like
select * from table where tags contains ('ec2' or 'associate')
or contains multiple values
select * from table where tags contains 's3' and 'rds'
What is the right query?
You can use the in-built find_in_set function.
find_in_set('s3',tags) > 0 and find_in_set('rds',tags) > 0
You can use the like operator:
select * from table
where
concat(',', tags, ',') like '%,s3,%'
and
concat(',', tags, ',') like '%,rds,%'
if in tags after each , there is a space then:
select * from table
where
concat(', ', tags, ',') like '%, s3,%'
and
concat(', ', tags, ',') like '%, rds,%'
This could be done using mysql regular expression function REGEXP_LIKE :
REGEXP_LIKE(tags, '(^|,)\s*ec2\s*($|,)' )
AND REGEXP_LIKE(tags, '(^|,)\s*associate\s*($|,)' )
As it does need not modify the compared value, it might perform better than the solution using LIKE.
Regex explanation :
(^|,) : beginning of the string OR a comma
\s* : 0 or more consecutive spaces
ec2 : string to search for
\s* : 0 or more consecutive spaces
($|,) : end of the string OR a comm
If you want an OR filter instead of AND, it can be expressed in a single function call, like :
REGEXP_LIKE(tags, '(^|,)\s*((ec2)|(associate))\s*($|,)' )

How to ignore symbols in SQL LIKE

I have a search module with SQL query like this:
SELECT FROM trilers WHERE title '%something%'
And when I search for keyword for example like "spiderman" it returns not found, but when I search for "spider-man" it returns my content (original row in MySQL is "spider-man").
How can I ignore all symbols like -, #, !, : and return content with "spiderman" and "spider-man" keywords at the same time?
What you can do is replace the characters you don't care about before the search takes place.
First iteration would look like this:
SELECT * FROM trilers WHERE REPLACE(title, '-', '') LIKE '%spiderman%'
This would ignore any '-'.
Next you would rap that with another REPLACE to include '#' like this:
SELECT * FROM trilers WHERE REPLACE(REPLACE(title, '-', ''), '#', '') LIKE '%spiderman%'
For all 3 ('!','-','#') you would just increase the Replace with another Replace like this:
SELECT * FROM trilers WHERE REPLACE(REPLACE(REPLACE(title, '-', ''), '#', ''),'!','') LIKE '%spiderman%'
You could try something like
SELECT * FROM trilers WHERE replace(title, '-', '') LIKE '%spiderman%'
The other answers involving using REPLACE are great, but if you don't care what characters appear between "spider" and "man" or how many characters there are between the two strings, you can use an additional wildcard in your expression:
SELECT * FROM Superheroes WHERE HeroName LIKE '%spider%man%';
If you want to match only one character, but allow any character, you can use the _ wildcard, which matches only one character:
SELECT * FROM Superheroes WHERE HeroName LIKE '%spider_man%';
This will match "spideryman" and "spideryman in la la land" but not "spiderysupereliteuberheroman".
If you have a limited number of possible symbols, a way to do it without REPLACE is to use a disjunctive expression:
SELECT * FROM Superheroes WHERE
HeroName LIKE '%spiderman%'
OR
HeroName LIKE '%spider-man%'
OR
HeroName LIKE '%spider#man%'
OR
HeroName LIKE '%spider!man%';
WHERE trilers REGEXP '[[:<:]]spider[-#!:]?man[[:>:]]'
Some discussion:
[[:<:]] -- word boundary
[-#!:] -- character set, matches any of them. ('-' must be first)
[-#!:]? -- optional -- so that 'spiderman' will still match
This, unlike the rest of the answers, will avoid matching
spidermaniac
Also, consider using FULLTEXT.
You should be able to use your search with a small update. You should be able to do something like: SELECT FROM trilers WHERE title LIKE '%spider%'. This should search for anything where spider is before or after something else like the hyphen (-)

SELECT to match special characters and case

Field X in table may contain special characters e.g hello!World and I would like to know if there is a way to match that with HelloWorld (Ignore case and special characters).
SELECT * FROM table WHERE X='Helloworld'
http://sqlfiddle.com/#!9/2afa1/1
if you need exaclty match of string:
SELECT *
FROM table1
WHERE x REGEXP '^hello[[:punct:],[:space:]]world$';
And if hello world could be a part of larger string:
SELECT *
FROM table1
WHERE x REGEXP 'hello[[:punct:],[:space:]]world';
What you can do is to replace all special characters like this:
SELECT * FROM table WHERE LOWER(REPLACE(X, '!', '')) = LOWER('HelloWorld');
Chain those replacements if you have to replace more:
SELECT * FROM table WHERE LOWER(REPLACE(REPLACE(X, '!', ''), '?', '')) = LOWER('HelloWorld');
If I understood your question right, you need to filter out non-ASCII characters? Please confirm whether this is true. In order to do that, have a look at REGEXP matching as in the comment link and this question.
Try something like
SELECT * FROM `table ` WHERE `X` REGEXP 'Helloworld';
REGEXP 'hello[^[:alpha:]]*world'
Notes:
This finds the string in the middle of other stuff; add ^ and $ to anchor to ends.
This assumes the non-alpha character(s) are between hello and world, not some other spot in the string.
This relies on the relevant collation to do (or not do) case folding.

Properly select actual non-alphanumeric characters in MySQL column

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;

Remove dots and hyphen on where clause MySQL

I need remove dots and hyphens on a string on compare in Where clause.
This is my query now:
Select * from tbl_sometable where some_column in ('10000000000', '1999999999')
But, the some_column have values like this: '129.012.120-01' and I need filter the values too, like the values in a clause "in".
How I can do this? I using MySQL, I see an example using Translate, but not work in MySQL.
Thanks and best Regards.
WHERE
REPLACE
(
REPLACE
(
some_column, "-", ""
), ".", ""
) in ('10000000000', '1999999999')
WHERE REPLACE(REPLACE(some_column, '.', ''), '-', '') in ('10000000000', '1999999999')