Roman Numeral Converter
WITH Roman (Numeral) AS
(select 'MCMLXXXVII' as Numeral from dual),
RomToDec (ThisVal, ThisOne, ThisDec, LastDec, Remaining, Pos) AS
(SELECT 0 as ThisVal, cast(null as varchar2(4000)) as ThisOne,
0 as ThisDec, 0 as LastDec,
Roman.Numeral as Remaining , length(Roman.Numeral) as Pos
FROM Roman
UNION ALL
SELECT
case
when RomToDec.ThisDec >= RomToDec.LastDec then RomToDec.ThisVal + ThisDec
else RomToDec.ThisVal-ThisDec
END as ThisVal,
substr(RomToDec.Remaining,length(RomToDec.Remaining),1) as ThisOne,
case substr(RomToDec.Remaining,length(RomToDec.Remaining),1)
when 'M' then 1000
when 'D' then 500
when 'C' then 100
when 'L' then 50
when 'X' then 10
when 'V' then 5
when 'I' then 1
else 0 END as ThisDec,
RomToDec.ThisDec as LastDec,
substr(RomToDec.Remaining,1,length(RomToDec.Remaining)-1) as Remaining,
length(RomToDec.Remaining)-1 as Pos
from RomToDec
where Pos >= 0
)
select thisVal
from RomToDec
where pos is null
THISVAL | 1987 |
---|