Google
Information Storage and Retrieval: Esperanto Spelling(SQL Puzzle 10)

Pages

Thursday, July 10, 2008

Esperanto Spelling(SQL Puzzle 10)

Problem: In Esperanto language, numbers are represented as "unu" for 1, "du" for 2, "tri" for 3, "kvar" for 4, "kvin" for 5, "ses" for 6, "sep" for 7, "ok" for 8, "nau" for 9, "dek" for 10. Numbers 11 to 19 are represented as "dek unu", "dek du","dek tri"......up to "dek nau". Numbers 20 to 29 are "dudek","dudek unu", "dudek tri".....up to "dudek nau". Similarly for all other numbers from 30 to 99 where 30 is "tridek", 40 is "kavrdek".....so on.. We have a table "esperanto_spell" having 2 columns as:

CREATE TABLE ESPERANTO_SPELL
(
NUMBERS NUMBER,
SPELL VARCHAR2(10 BYTE)
)

with values as:
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (1, 'unu');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (2, 'du');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (3, 'tri');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (4, 'kvar');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (5, 'kvin');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (6, 'ses');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (7, 'sep');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (8, 'ok');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (9, 'nau');
Insert into ESPERANTO_SPELL (NUMBERS, SPELL) Values (10, 'dek');

Write an SQL query to return the Esperanto Spelling of a given number.
(The given number will be between 1 and 99).

Solution :


select
(select
decode(spell^^'dek ','unudek ','dek ',spell^^'dek ')
from esperanto_spell
where numbers=trunc(&given_number/10) )^^(select spell from esperanto_spell where numbers=mod(&given_number,10)) as converted
from dual

(^^ represents concatenation operator i.e pipes. Due to some print format the pipes are not being reflected. Replace ^^ with pipes while running)

No comments: