Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Strings ignoring accents in SQL (ORACLE)

Tags:

sql

oracle

I would like to know if there is an easy way to compare two text values ignoring the accents and upper case. Im working with an Oracle database. I already searched for an answer but honestly I don't understand what they proposed in here Accent and case insensitive collation in Oracle with LIKE. I tried that and it didn't work for me. Basically all I want to do is two compare to text values like 'pepé' and 'pepe' and obtain true as answer.

is it possible to do it without the LIKE instruction?

Thank You!

like image 506
Tiasn Avatar asked Feb 28 '16 22:02

Tiasn


People also ask

How can I compare two strings without case-sensitive 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' .

How do you do case insensitive comparison 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.

Is Oracle string comparison case-sensitive?

For binary strings ( BINARY , VARBINARY , BLOB ), comparisons use the numeric values of the bytes in the operands; this means that for alphabetic characters, comparisons are case-sensitive.

How do I find and replace special characters in Oracle?

You can replace special characters using the Oracle REPLACE function. To replace both carriage return and new line characters, you must use nested REPLACE functions. If you want to replace a lot of special characters, using many nested REPLACE functions can get messy and could have performance impacts.


1 Answers

Oracle Setup:

CREATE TABLE TABLE_NAME ( value ) AS
SELECT 'pepé' FROM DUAL;

-- Not necessary to create an index but it can speed things up.
CREATE INDEX value_without_accent_idx
  ON TABLE_NAME ( CONVERT( value, 'US7ASCII' ) );

Query:

SELECT *
FROM   table_name
WHERE  CONVERT( value, 'US7ASCII' ) = 'pepe';

Output:

VALUE
-----
pepé  
like image 135
MT0 Avatar answered Oct 06 '22 00:10

MT0