214


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%' 
1

9 Answers 9

397

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%') 
Sign up to request clarification or add additional context in comments.

6 Comments

I think you should always compare strings capitalized (UPPER) as best practice. Google "turkish i"
Like to know does your answer has any performance issue by converting a column value to UPPER or LOWER case then using the LIKE to search ?
Actually you would have to compare both UPPER and LOWER variants because some characters have different representations in upper case but the same representation in lower case. For other characters, the opposite may be true. Java mentions the Georgian alphabet specifically as reason for doing an additional toLowerCase() in its case-insensitive comparison.
Unfortunately, this approach causes a full table scan as described in this post: alvinalexander.com/sql/…. An index search can not be applied, as the filtered column is modified by the UPPER/LOWER function.
Setting a collation (before creating the index) seems to be the better approach if query performance with upper/lower is not sufficient.
|
55

Like this.

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

In postgresql.

2 Comments

This question is tagged sql-server so a postgres answer isn't relevant.
Thanks. I applied same logic on Apache Spark and works correctly
13

See this similar question and answer to searching with case insensitivity - 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%' 

3 Comments

it din't work I get SQL query not properly ended before Collate
@Miguel-F.. it is working fine but how does it differ from SELECT DISTINCT COL_NAME FROM myTable WHERE COL_NAME LIKE '%priceorder%' because this is also working fine for me..
I would advice using 'SQL_Latin1_General_Cp1_CI_AS_KI_WI' instead. It's not case sensetive for a seach.
5

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%') 

Comments

4

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.

Comments

2

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

Comments

1

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.

1 Comment

This shows up as invalid syntax in the 2 SQL apps I use
1

LIKE and ILIKE allow pattern matching within character-based column data. Their syntax is identical, but LIKE is case-sensitive, while ILIKE is case-insensitive. i am using postgresql, it work fine for me. i.e

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

Comments

0

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
Table design & collation used : https://prnt.sc/1vjx3h5.

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

1 Comment

My coalition is utf8_general_ci and the queries are still case sensitive

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.