Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle DB: How can I write query ignoring case?

As I had written in title, I have SQL query, run on Oracle DB, lets say:

SELECT * FROM TABLE WHERE TABLE.NAME Like 'IgNoReCaSe' 

If I would like, that the query would return either "IGNORECASE", "ignorecase" or combinations of them, how can this be done?

like image 394
zeroDivisible Avatar asked Jun 23 '09 10:06

zeroDivisible


People also ask

How do you write a case-insensitive query in Oracle?

Starting with Oracle Database 10g Release 2 you can use the initialization parameters NLS_COMP and NLS_SORT to render all string comparisons case insensitive. We need to set the NLS_COMP parameter to LINGUISTIC, which will tell the database to use NLS_SORT for string comparisons.

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.

How do I compare and ignore a case in SQL?

To do a case-insensitive comparison, use the ILIKE keyword; e.g., column ILIKE 'aBc' and column ILIKE 'ABC' both return TRUE for 'abc' . In contrast, MySQL and MS SQL Server have case-insensitive behaviors by default. This means WHERE column = 'abc' returns TRUE for e.g., 'abc' , 'ABC' , or 'aBc' .

What is the alternative for CASE statement in Oracle?

The Oracle functions CASE, DECODE, and COALESCE all perform similar functionality. They can transform a value into another value.


2 Answers

Select * from table where upper(table.name) like upper('IgNoreCaSe'); 

Alternatively, substitute lower for upper.

like image 76
Hooloovoo Avatar answered Sep 20 '22 15:09

Hooloovoo


Use ALTER SESSION statements to set comparison to case-insensitive:

alter session set NLS_COMP=LINGUISTIC; alter session set NLS_SORT=BINARY_CI; 

If you're still using version 10gR2, use the below statements. See this FAQ for details.

alter session set NLS_COMP=ANSI; alter session set NLS_SORT=BINARY_CI; 
like image 39
devio Avatar answered Sep 21 '22 15:09

devio