views:

3298

answers:

7

Hello,

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?

Is this possible?

+4  A: 

You can use either lower or upper function on both sides of the where condition

joe
Thank You for such an fast answer. I am wondering, because my query selects about 12 fields from really huge table, so I would need about 20 uses of upper function - wouldn't it be a performance hit?
zeroDivisible
If you know the exact words you want to check against, you could use an IN statement (SELECT * FROM TABLE WHERE UPPER(NAME) IN (UPPER('Name1'), UPPER('Name2')); or if the names all start the same you could do ths with a wildcard (SELECT * FROM TABLE WHERE UPPER(NAME) LIKE UPPER('Search%');)
Hooloovoo
The use of functions like upper is never a performance hit if you're talking about the time it takes Oracle to perform the operation - the time it takes the cpu to do the conversion is trivial compared to the time it takes to load the data pages from the cache, never mind getting it from the disk. However, using lower or upper will prevent your query from using any indexes on those columns, unless you created indexes that used those functions. But LIKE could possibly prevent index use as well, particularly if the first character in the expression is a wildcard.
Steve Broberg
+2  A: 
Select * from table where upper(table.name) like upper('IgNoreCaSe');

Alternatively, substitute lower for upper.

Hooloovoo
+2  A: 

you can use the upper() function in your query, and to increase performance you can use a function-base index

 CREATE INDEX upper_index_name ON table(upper(name))

there is a lttle configuratin that may need to be done for these to work:

user level:

your db user will require 'query rewrite' and 'global query rewrite' priveleges

and two properties on the session or system level:

QUERY_REWRITE_ENABLED=TRUE and QUERY_REWRITE_INTEGRITY=TRUSTED

you can find some info here and here

akf
+6  A: 

You can use ALTER SESSION statements to set comparison to case-insensitive. See this FAQ.

alter session set NLS_COMP=ANSI;
alter session set NLS_SORT=BINARY_CI;
devio
if zeroDevisible is on 10gR2 or greater, this is much better than function-based indexes, as it will cover all of the 12 fields in the query without the overhead of all those indexes. one potential issue is that the user needs to alter the session every time.
akf
I'm not sure what overhead you're talking about. If you create a "normal" index, then change the NLS_COMP and NLS_SORT, Oracle won't be able to use the index any longer to find the data in question. So you would end up creating NLS setting-specific indexes for whatever columns would be appropriate. It's not obvious to me how that generates any more or less overhead than function-based indexes (FBI). Obviously, if you want all queries to be case insensitive, there is no need to maintain an index on a column and a FBI on UPPER(column) (or to maintain indexes for different NLS settings)
Justin Cave
+1  A: 

...also do the conversion to upper or lower outside of the query:

tableName:= UPPER(someValue || '%');

...

Select * from table where upper(table.name) like tableName
ozczecho
+1  A: 

You could also use Regular Expressions:

SELECT * FROM TABLE WHERE REGEXP_LIKE (TABLE.NAME,'IgNoReCaSe','i');

kMAP
A: 

Also don't forget the obvious, does the data in the tables need to have case? You could only insert rows already in lower case (or convert the existing DB rows to lower case) and be done with it right from the start.

Gandalf