SQL- Ignore case while searching for a string

SqlSql ServerCase InsensitiveSql LikeIgnore Case

Sql Problem Overview



I have the following data in a Table
PriceOrderShipped
PriceOrderShippedInbound
PriceOrderShippedOutbound

In SQL I need to write a query which searches for a string in a table. While searching for a string it should ignore case. For the below mentioned SQL query

SELECT DISTINCT COL_NAME FROM myTable WHERE COL_NAME LIKE '%PriceOrder%' 

gives all the above data, whereas

SELECT DISTINCT COL_NAME FROM myTable WHERE COL_NAME LIKE '%Priceorder%' 

doesn't give.

Eg. when I search for 'PriceOrder' or 'priceOrder' it works but 'priceorder' or 'Priceorder' doesn't work. I have tried with the below query using COLLATE, but its not working. Do let me know where im going wrong.

SELECT DISTINCT COL_NAME FROM myTable WHERE 
COL_NAME COLLATE latin1_general_cs LIKE '%Priceorder%'

Sql Solutions


Solution 1 - Sql

Use something like this -

SELECT DISTINCT COL_NAME FROM myTable WHERE UPPER(COL_NAME) LIKE UPPER('%PriceOrder%')

or

SELECT DISTINCT COL_NAME FROM myTable WHERE LOWER(COL_NAME) LIKE LOWER('%PriceOrder%')

Solution 2 - Sql

Like this.

SELECT DISTINCT COL_NAME FROM myTable WHERE COL_NAME iLIKE '%Priceorder%'

In postgresql.

Solution 3 - Sql

See this similar question and answer to searching with case insensitivity - https://stackoverflow.com/questions/1224364/sql-server-ignore-case-in-a-where-expression

Try using something like:

SELECT DISTINCT COL_NAME 
FROM myTable 
WHERE COL_NAME COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%priceorder%'

Solution 4 - Sql

You should probably use SQL_Latin1_General_Cp1_CI_AS_KI_WI as your collation. The one you specify in your question is explictly case sensitive.

You can see a list of collations here.

Solution 5 - Sql

The like operator is not case sensitive in almost all the SQL compilers. If you are still not getting case sensitive results then go with iLike operator. Also you can compare it by changing the case using Upper() method.

SELECT DISTINCT COL_NAME FROM myTable WHERE COL_NAME iLIKE '%PriceOrder%' 

or

SELECT DISTINCT COL_NAME FROM myTable WHERE UPPER(COL_NAME) LIKE UPPER('%PriceOrder%')
SELECT DISTINCT COL_NAME FROM myTable WHERE LOWER(COL_NAME) LIKE LOWER('%PriceOrder%')

Solution 6 - Sql

If someone using ORACLE can be changes at database level itself

ALTER SESSION SET NLS_COMP=LINGUISTIC;  
ALTER SESSION SET NLS_SORT=BINARY_CI;  

Note:- Oracle

Solution 7 - Sql

Here's the solution which I use.

SELECT * FROM TABLE WHERE COLUMN ~* '.*search_keyword_any_case.*'

This will also check for substring. Overall the best solution that I came across from the internet. I use the same to implement substring case insensitive search for table's columns.

Solution 8 - Sql

ALTERNATE & BEST SOLUTION:
You can also use a specific "collation" like utf8 > utf8_unicode_ci. Then ALL QUERIES will be insensitive to case. But the data can be in upper case in some places. So just be sure that there is no twin with different cases possible (like using a UNIQUE column to be sure).

QUERY Example:
https://prnt.sc/1vjwxd1<br> Table design & collation used : https://prnt.sc/1vjx3h5.

"A good design of Database can save you a lot of work in queries" - Me :)

Solution 9 - Sql

As I experienced in my projects for me like results in case-sensitive search and LIKE results case-insensitive. I don't know how this works but I experienced this in my projects. using hibernate

USERS

  1. jhon
  2. Jhonny
  3. jHames

when I used

from Users userName LIKE 'jh%'

results all three and when

 from Users userName like 'jh%'

results only jhon and jhonny

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshockwaveView Question on Stackoverflow
Solution 1 - SqlAditya KakirdeView Answer on Stackoverflow
Solution 2 - SqlHangkView Answer on Stackoverflow
Solution 3 - SqlMiguel-FView Answer on Stackoverflow
Solution 4 - Sqluser806549View Answer on Stackoverflow
Solution 5 - SqlCodemakerView Answer on Stackoverflow
Solution 6 - Sqlblack_pottery_beautyView Answer on Stackoverflow
Solution 7 - SqlReGoView Answer on Stackoverflow
Solution 8 - SqlVinikView Answer on Stackoverflow
Solution 9 - SqlMohammedsameer InamdarView Answer on Stackoverflow