How to Convert Accented Characters into Non Accented Ones – Part 2

In Part 1, I discussed an approach to convert accented (diacritic characters) into non accented ones, using Oracle’s inbuilt nlssort functionality.  I also mentioned the flaw with it, in that it loses capitalisation.

Another approach is to use Oracle’s TRANSLATE function, this replaces a sequence of characters in a string with another sequence of characters.  So the only problem is creating a list of the diacritic characters and their equivalents.  Well fortunately, I’ve done that for you, shown in the example below, and also show how you could wrap things up in an easy to use function.

Note : There are many diacritic characters in the world, so I’ve picked mainly Latin ones here, but you can obviously add to the example how you see fit. Your list may also depend on if you are using a Unicode character-set or not, but you can tweak the list to whatever suits your needs.


SQL> select
  2    translate(
  3  	 'Necht již hríšné saxofony dáblu rozezvucí sín údesnými tóny waltzu, tanga a quickstepu', -- Example text
  4  	 'ÁÀÄÂĀÅǍĄĂÃĈĆÇČĐĎÊËÉÈĚĒĖĘĞĢĜĤÌĮĪÏÎÍĴĶĻĹĽÑŃŇŅÒÖÓØŐÕÔŔŘŠŚŜŞȘŤŢȚÜÙÚÛŪŨŲŮŰŴÝŸŶŹŽŻäàāăąǎåáâãćçčĉđďèéêëěęėēĝğģĥìīįíîïıĵķĺłļľñńņňőøöòóôõřŕșšşśŝťțţūůųũüùúûűŵŷýÿžżź',
  5  	 'AAAAAAAAAACCCCDDEEEEEEEEGGGHIIIIIIJKLLLNNNNOOOOOOORRSSSSSTTTUUUUUUUUUWYYYZZZaaaaaaaaaaccccddeeeeeeeeggghiiiiiiijkllllnnnnooooooorrssssstttuuuuuuuuuwyyyzzz'
  6    ) accent_less
  7  from dual;

ACCENT_LESS
--------------------------------------------------------------------------------------
Necht jiz hrisne saxofony dablu rozezvuci sin udesnymi tony waltzu, tanga a quickstepu

SQL> 
SQL> create or replace function AccentLess(pStr varchar2) return varchar2 is
  2  begin
  3    return translate(
  4  		    pStr
  5      		'ÁÀÄÂĀÅǍĄĂÃĈĆÇČĐĎÊËÉÈĚĒĖĘĞĢĜĤÌĮĪÏÎÍĴĶĻĹĽÑŃŇŅÒÖÓØŐÕÔŔŘŠŚŜŞȘŤŢȚÜÙÚÛŪŨŲŮŰŴÝŸŶŹŽŻäàāăąǎåáâãćçčĉđďèéêëěęėēĝğģĥìīįíîïıĵķĺłļľñńņňőøöòóôõřŕșšşśŝťțţūůųũüùúûűŵŷýÿžżź',
  6  	    	'AAAAAAAAAACCCCDDEEEEEEEEGGGHIIIIIIJKLLLNNNNOOOOOOORRSSSSSTTTUUUUUUUUUWYYYZZZaaaaaaaaaaccccddeeeeeeeeggghiiiiiiijkllllnnnnooooooorrssssstttuuuuuuuuuwyyyzzz'
  7  	      );
  8  end;
  9  /

Function created.

SQL> 
SQL> select AccentLess('Necht již hríšné saxofony dáblu rozezvucí sín údesnými tóny waltzu, tanga a quickstepu') accent_less
  2  from dual;

ACCENT_LESS
--------------------------------------------------------------------------------------
Necht jiz hrisne saxofony dablu rozezvuci sin udesnymi tony waltzu, tanga a quickstepu

For those of you wondering, the test text is a Czech pangram (a sentence using all letters of the particular language’s alphabet), in this case it translates to “Let the sinful saxophones of devils finally make the hall resonate with the frightful tones of waltz, tango and quickstep.”

Nvl versus Coalesce, and the winner is…

Most Oracle developers I know use NVL, rather than COALESCE for returning the first not null value of two arguments.  Most will cite it’s for habitual, historic reasons (COALESCE was introduced later in Oracle’s history) or because it’s shorter to type.

Well NVL and COALESCE do the same thing anyway, so why would I change?

Well, there’s one very good reason why you should the SQL standards function COALESCE over Oracle’s proprietary NVL….
Continue reading

Faster Decimal to Binary Function

There was recently a post on the Oracle forums by a person who needed to convert decimal values to a binary string. They had found the standard library PL/SQL function that you often see on Oracle sites, but its slow performance was giving him headaches. It was an interesting thread as many people offered solutions, and it showed the ingenuity of people, in coming up with alternatives. In this article I’ll show some of my efforts, and my final function which, to my pleasure, was the fastest in the thread.

Read more…