The default behaviour of LIKE
and the other comparison operators, =
etc is case-sensitive.
Is it possible make them case-insensitive?
The default behaviour of LIKE
and the other comparison operators, =
etc is case-sensitive.
Is it possible make them case-insensitive?
From Oracle 12c R2 you could use
COLLATE operator
:Demo:
db<>fiddle demo
you can do something like that:
maybe you can try using
There are 3 main ways to perform a case-insensitive search in Oracle without using full-text indexes.
Ultimately what method you choose is dependent on your individual circumstances; the main thing to remember is that to improve performance you must index correctly for case-insensitive searching.
1. Case your column and your string identically.
You can force all your data to be the same case by using
UPPER()
orLOWER()
:or
If
column_1
is not indexed onupper(column_1)
orlower(column_1)
, as appropriate, this may force a full table scan. In order to avoid this you can create a function-based index.If you're using LIKE then you have to concatenate a
%
around the string you're searching for.This SQL Fiddle demonstrates what happens in all these queries. Note the Explain Plans, which indicate when an index is being used and when it isn't.
2. Use regular expressions.
From Oracle 10g onwards
REGEXP_LIKE()
is available. You can specify the _match_parameter_'i'
, in order to perform case-insensitive searching.In order to use this as an equality operator you must specify the start and end of the string, which is denoted by the carat and the dollar sign.
In order to perform the equivalent of LIKE, these can be removed.
Be careful with this as your string may contain characters that will be interpreted differently by the regular expression engine.
This SQL Fiddle shows you the same example output except using REGEXP_LIKE().
3. Change it at the session level.
The NLS_SORT parameter governs the collation sequence for ordering and the various comparison operators, including
=
and LIKE. You can specify a binary, case-insensitive, sort by altering the session. This will mean that every query performed in that session will perform case-insensitive parameters.There's plenty of additional information around linguistic sorting and string searching if you want to specify a different language, or do an accent-insensitive search using BINARY_AI.
You will also need to change the NLS_COMP parameter; to quote:
The default value of NLS_COMP is BINARY; but, LINGUISTIC specifies that Oracle should pay attention to the value of NLS_SORT:
So, once again, you need to alter the session
As noted in the documentation you may want to create a linguistic index to improve performance
Since 10gR2, Oracle allows to fine-tune the behaviour of string comparisons by setting the
NLS_COMP
andNLS_SORT
session parameters:You can also create case insensitive indexes:
This information was taken from Oracle case insensitive searches. The article mentions
REGEXP_LIKE
but it seems to work with good old=
as well.In versions older than 10gR2 it can't really be done and the usual approach, if you don't need accent-insensitive search, is to just
UPPER()
both the column and the search expression.