Case insensitive search by converting case
select athlete_name
from olym.olym_athletes
where upper ( athlete_name ) like upper ( '%barry%' )
order by athlete_name
ATHLETE_NAME |
---|
BARRY, Kevin |
BARRY, William Louis |
DAGGER, Barry Edward |
DANCER, Barry |
DAVIS, Barry Alan |
DEMET-BARRY, Deirdre |
KELLY, Barry |
MAGEE, Arthur Barry |
MAISTER, Barry John |
WEITZENBERG, Charles Barry |
Case insensitive search using collate
select athlete_name
from olym.olym_athletes
where athlete_name like '%barry%'
collate binary_ci
order by athlete_name
ATHLETE_NAME |
---|
BARRY, Kevin |
BARRY, William Louis |
DAGGER, Barry Edward |
DANCER, Barry |
DAVIS, Barry Alan |
DEMET-BARRY, Deirdre |
KELLY, Barry |
MAGEE, Arthur Barry |
MAISTER, Barry John |
WEITZENBERG, Charles Barry |
Case insensitive range comparison
select count (*)
from olym.olym_athletes
where athlete_name >= 'n'
collate binary_ci
COUNT(*) |
---|
8203 |
Exact text range comparison
select count (*)
from olym.olym_athletes
where athlete_name >= 'n'
COUNT(*) |
---|
12 |
select athlete_name
from olym.olym_athletes
where athlete_name >= 'n'
ATHLETE_NAME |
---|
ÖRSTED, Hans-Henrik |
ÖRTEGREN, Ruben |
ÖRVIG, Erik |
ÖRVIG, Olav |
ÖRVIG, Thor |
ÖSTENSEN, Östen |
ÖSTERVOLD, Henrik |
ÖSTERVOLD, Jan Olsen |
ÖSTERVOLD, Kristian Olsen |
ÖSTERVOLD, Ole Olsen |
ÖSTMO, Ole |
ÖSTRAND, Per-Olof |
Accent insensitve search
select substr (
athlete_name,
instr ( athlete_name, ', ' ) + 2
) given_names,
athlete_name
from olym.olym_athletes
where athlete_name like '%helene%'
collate binary_ai
order by given_names
GIVEN_NAMES | ATHLETE_NAME |
---|---|
Helene | CORTIN, Helene |
Helene | JUNKER, Helene |
Helene | MADISON, Helene |
Helene | MAYER, Helene |
Helene "Leni" | SCHMIDT, Helene "Leni" |
Hélène | PREVOST, Hélène |
Marie-Helene | PREMONT, Marie-Helene |
Change how accented characters are sorted
select substr (
athlete_name, instr ( athlete_name, ',' ) + 2
) given_names,
athlete_name
from olym.olym_athletes
where athlete_name like '%helene%'
collate binary_ai
order by given_names
collate binary_ai
GIVEN_NAMES | ATHLETE_NAME |
---|---|
Helene | CORTIN, Helene |
Helene | JUNKER, Helene |
Helene | MADISON, Helene |
Helene | MAYER, Helene |
Hélène | PREVOST, Hélène |
Helene "Leni" | SCHMIDT, Helene "Leni" |
Marie-Helene | PREMONT, Marie-Helene |
select substr (
athlete_name, instr ( athlete_name, ',' ) + 2
) collate binary_ai given_names,
athlete_name
from olym.olym_athletes
where athlete_name like '%helene%'
collate binary_ai
order by given_names
GIVEN_NAMES | ATHLETE_NAME |
---|---|
Helene | CORTIN, Helene |
Helene | JUNKER, Helene |
Helene | MADISON, Helene |
Helene | MAYER, Helene |
Hélène | PREVOST, Hélène |
Helene "Leni" | SCHMIDT, Helene "Leni" |
Marie-Helene | PREMONT, Marie-Helene |
Sort strings in Czech
select substr (
athlete_name, instr ( athlete_name, ',' ) + 2
) collate czech as given_names,
athlete_name
from olym.olym_athletes
where athlete_name like '%, helene%'
collate binary_ai
order by given_names
GIVEN_NAMES | ATHLETE_NAME |
---|---|
Helene | CORTIN, Helene |
Helene | JUNKER, Helene |
Helene | MADISON, Helene |
Helene | MAYER, Helene |
Helene "Leni" | SCHMIDT, Helene "Leni" |
Hélène | PREVOST, Hélène |
Sort strings in French
select substr (
athlete_name, instr ( athlete_name, ',' ) + 2
) collate french as given_names,
athlete_name
from olym.olym_athletes
where athlete_name like '%, helene%'
collate binary_ai
order by given_names
GIVEN_NAMES | ATHLETE_NAME |
---|---|
Helene | CORTIN, Helene |
Helene | JUNKER, Helene |
Helene | MADISON, Helene |
Helene | MAYER, Helene |
Hélène | PREVOST, Hélène |
Helene "Leni" | SCHMIDT, Helene "Leni" |
View the sort key for strings in different collations
select substr (
athlete_name, instr ( athlete_name, ',' ) + 2, 6
) as given_names,
nlssort (
substr (
athlete_name, instr ( athlete_name, ',' ) + 2, 6
) collate french
) as french_sort_value,
nlssort (
substr (
athlete_name, instr ( athlete_name, ',' ) + 2, 6
) collate czech
) as czech_sort_value
from olym.olym_athletes
where athlete_name like '%, helene%'
collate binary_ai
order by given_names
GIVEN_NAMES | FRENCH_SORT_VALUE | CZECH_SORT_VALUE |
---|---|---|
Helene | 37284B2855280001020202020200 | 37284B2855280002010101010100 |
Helene | 37284B2855280001020202020200 | 37284B2855280002010101010100 |
Helene | 37284B2855280001020202020200 | 37284B2855280002010101010100 |
Helene | 37284B2855280001020202020200 | 37284B2855280002010101010100 |
Helene | 37284B2855280001020202020200 | 37284B2855280002010101010100 |
Hélène | 37284B2855280001040206020200 | 37284B55280002030100E8010100 |
Column-level collation
create table olym_athletes (
id number
not null,
athlete_name varchar2(255)
collate binary_ai
not null,
athlete_gender varchar2(10)
collate binary_ci
not null
) default collation binary_ai
Table created.
Column collation settings
select table_name,
default_collation,
column_name,
collation
from user_tables
join user_tab_cols
using ( table_name )
where table_name = 'OLYM_ATHLETES'
TABLE_NAME | DEFAULT_COLLATION | COLUMN_NAME | COLLATION |
---|---|---|---|
OLYM_ATHLETES | BINARY_AI | ID | - |
OLYM_ATHLETES | BINARY_AI | ATHLETE_NAME | BINARY_AI |
OLYM_ATHLETES | BINARY_AI | ATHLETE_GENDER | BINARY_CI |
insert into olym_athletes
select * from olym.olym_athletes
21415 row(s) inserted.
commit
Statement processed.
create index athlete_name_i
on olym_athletes ( athlete_name )
Index created.
Change a column's collation
alter table olym_athletes
modify athlete_name collate french_ci
ORA-43923: The column cannot be modified to the specified collation.More Details: https://docs.oracle.com/error-help/db/ora-43923
Remove the index
drop index athlete_name_i
Index dropped.
alter table olym_athletes
modify athlete_name collate french_ci
Table altered.
select athlete_name from olym_athletes
where athlete_name like '%barry%'
ATHLETE_NAME |
---|
WEITZENBERG, Charles Barry |
DAGGER, Barry Edward |
DANCER, Barry |
DAVIS, Barry Alan |
DEMET-BARRY, Deirdre |
KELLY, Barry |
MAGEE, Arthur Barry |
MAISTER, Barry John |
BARRY, Kevin |
BARRY, William Louis |
Change the table's default collation
alter table olym_athletes
default collation german_ci
Table altered.
Add virtual columns with different collations
alter table olym_athletes
add (
athlete_name_french as
( athlete_name collate french ),
athlete_name_czech as
( athlete_name collate czech ),
athlete_name_binary_ci as
( athlete_name collate binary_ci )
)
Table altered.
View new default and virtual column collations
select table_name,
default_collation,
column_name,
collation
from user_tables
join user_tab_cols
using ( table_name )
where table_name = 'OLYM_ATHLETES'
TABLE_NAME | DEFAULT_COLLATION | COLUMN_NAME | COLLATION |
---|---|---|---|
OLYM_ATHLETES | GERMAN_CI | ID | - |
OLYM_ATHLETES | GERMAN_CI | ATHLETE_NAME | FRENCH_CI |
OLYM_ATHLETES | GERMAN_CI | ATHLETE_GENDER | BINARY_CI |
OLYM_ATHLETES | GERMAN_CI | ATHLETE_NAME_FRENCH | FRENCH |
OLYM_ATHLETES | GERMAN_CI | ATHLETE_NAME_CZECH | CZECH |
OLYM_ATHLETES | GERMAN_CI | ATHLETE_NAME_BINARY_CI | BINARY_CI |
Check the session's current settings
select *
from nls_session_parameters
where parameter in ( 'NLS_COMP', 'NLS_SORT' )
PARAMETER | VALUE |
---|---|
NLS_SORT | BINARY |
NLS_COMP | BINARY |
Change NLS_SORT
alter session set nls_sort = binary_ai
Statement processed.
Change NLS_COMP
alter session set nls_comp = linguistic
Statement processed.
Remove virtual columns
alter table olym_athletes
drop (
athlete_name_french,
athlete_name_czech,
athlete_name_binary_ci
)
Table altered.
Set a column to use session NLS settings
alter table olym_athletes
modify athlete_name collate using_nls_comp
Table altered.
Changing comparison rules with session settings
begin
execute immediate 'alter session set nls_sort = binary_ci';
execute immediate 'alter session set nls_comp = linguistic';
dbms_output.put_line ( 'Case-insensitive search' );
for rws in (
select *
from olym_athletes
where athlete_name like '%, helene%'
) loop
dbms_output.put_line ( rws.athlete_name );
end loop;
execute immediate 'alter session set nls_sort = binary_ai';
dbms_output.put_line ( 'Accent- and case-insensitive search' );
for rws in (
select *
from olym_athletes
where athlete_name like '%, helene%'
) loop
dbms_output.put_line ( rws.athlete_name );
end loop;
execute immediate 'alter session set nls_comp = binary';
dbms_output.put_line ( 'Exact match' );
for rws in (
select *
from olym_athletes
where athlete_name like '%, helene%'
) loop
dbms_output.put_line ( rws.athlete_name );
end loop;
end;
Statement processed.
Case-insensitive search
CORTIN, Helene
JUNKER, Helene
MADISON, Helene
MAYER, Helene
SCHMIDT, Helene "Leni"
Accent- and case-insensitive search
CORTIN, Helene
JUNKER, Helene
MADISON, Helene
MAYER, Helene
PREVOST, Hélène
SCHMIDT, Helene "Leni"
Exact match
Set a column to use NLS_SORT session settings
alter table olym_athletes
modify athlete_name collate using_nls_sort
Table altered.
Changing comparison rules using NLS_SORT
begin
execute immediate 'alter session set nls_sort = binary_ai';
execute immediate 'alter session set nls_comp = binary';
dbms_output.put_line ( 'Accent- and case-insensitive search' );
for rws in (
select *
from olym_athletes
where athlete_name like '%, helene%'
) loop
dbms_output.put_line ( rws.athlete_name );
end loop;
end;
Statement processed.
Accent- and case-insensitive search
CORTIN, Helene
JUNKER, Helene
MADISON, Helene
MAYER, Helene
PREVOST, Hélène
SCHMIDT, Helene "Leni"
Set a column to use the accent insenstive version of NLS_SORT
alter table olym_athletes
modify athlete_name collate using_nls_sort_ai
Table altered.
Accent-insensitive search of NLS_SORT collation
begin
execute immediate 'alter session set nls_sort = binary';
execute immediate 'alter session set nls_comp = binary';
dbms_output.put_line ( 'Accent-insensitive search' );
for rws in (
select *
from olym_athletes
where athlete_name like '%, helene%'
) loop
dbms_output.put_line ( rws.athlete_name );
end loop;
end;
Statement processed.
Accent-insensitive search
CORTIN, Helene
JUNKER, Helene
MADISON, Helene
MAYER, Helene
PREVOST, Hélène
SCHMIDT, Helene "Leni"
Create a linguistic index
create index alth_name_nls
on olym_athletes (
nlssort ( athlete_name, 'nls_sort = binary_ai')
)
Index created.
Create a linguistic index using collate
create index athlete_name_ci
on olym_athletes (
athlete_name
collate binary_ci
)
Index created.
select *
from olym_athletes
where athlete_name = 'latynina, larisa'
collate binary_ci
ID | ATHLETE_NAME | ATHLETE_GENDER |
---|---|---|
10549 | LATYNINA, Larisa | Women |
Get the execution plan
select *
from dbms_xplan.display_cursor ( sql_id => '49d0cd7mkrq59', format => 'BASIC LAST' )
PLAN_TABLE_OUTPUT |
---|
EXPLAINED SQL STATEMENT: |
------------------------ |
select * from olym_athletes where athlete_name = 'latynina, |
larisa' collate binary_ci |
Plan hash value: 3896324904 |
--------------------------------------------------------------- |
| Id | Operation | Name | |
--------------------------------------------------------------- |
| 0 | SELECT STATEMENT | | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| OLYM_ATHLETES | |
| 2 | INDEX RANGE SCAN | ATHLETE_NAME_CI | |
--------------------------------------------------------------- |
Collating long strings
select *
from olym_athletes
where rpad ( athlete_name, 30000, 'trailing chars' ) =
rpad ( 'LATYNINA, Larisa', 30000, 'trailing chars' )
|| 'extra chars'
collate french_ai
ORA-12742: unable to create the collation keyMore Details: https://docs.oracle.com/error-help/db/ora-12742