Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT LIKE (Insensitive casing)

I am trying to execute the sql query:

select * from table where column like '%value%'; 

But the data is saved as 'Value' ( V is capital ).

When I execute this query i don't get any rows. How do i make the call such that, it looks for 'value' irrespective of the casing of the characters ?

like image 254
user2583714 Avatar asked Sep 17 '13 15:09

user2583714


People also ask

Is like in SQL case insensitive?

LIKE performs case-insensitive substring matches if the collation for the expression and pattern is case-insensitive.

How do I ignore case sensitive in SQL LIKE operator?

Using LOWER( ad UPPER() functions for case sensitive queries In the similar fashion UPPER() and LOWER() functions can be used in the LIKE clause for getting similar records and making the search insensitive in the table or database having collation that shows CS that is case sensitive in its collation.

How do I SELECT case sensitive in SQL?

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.

How do you ignore a case in a query?

Another way for case-insensitive matching is to use a different “collation”. The default collations used by SQL Server and MySQL do not distinguish between upper and lower case letters—they are case-insensitive by default. The logic of this query is perfectly reasonable but the execution plan is not: DB2.


2 Answers

use LOWER Function in both (column and search word(s)). Doing it so, you assure that the even if in the query is something like %VaLuE%, it wont matter

select qt.* from query_table qt where LOWER(column_name) LIKE LOWER('%vAlUe%'); 
like image 185
JGutierrezC Avatar answered Sep 21 '22 17:09

JGutierrezC


If you want this column be case insensitive :

ALTER TABLE `schema`.`table`  CHANGE COLUMN `column` `column` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'; 

Thus, you don't have to change your query.

And the MySQL engine will process your query quicker than using lower() function or any other tricks.

And I'm not sure that using lower function will be a good solution for index searching performance.

like image 31
kmas Avatar answered Sep 21 '22 17:09

kmas