Generate Roman Numerals

  • Comments posted to this topic are about the item Generate Roman Numerals

  • Maybe some programmers will appreciate my attempt at a simpler version.

    DECLARE @i int, @C varchar(10);

    SET @i = 133;

    SET @C = RIGHT('0000000000'+CAST(@i AS varchar(10)),10);

    SELECT @C,

    REPLICATE('M',CAST(LEFT(@c,7) AS int))

    +CASE substring(@c,8,1) WHEN '1' THEN 'C' WHEN '2' THEN 'CC' WHEN '3' THEN 'CCC' WHEN '4' THEN 'CD' WHEN '5' THEN 'D' WHEN '6' THEN 'DC' WHEN '7' THEN 'DCC' WHEN '8' THEN 'DCCC' WHEN '9' THEN 'CM' ELSE '' END

    +CASE substring(@c,9,1) WHEN '1' THEN 'X' WHEN '2' THEN 'XX' WHEN '3' THEN 'XXX' WHEN '4' THEN 'XL' WHEN '5' THEN 'L' WHEN '6' THEN 'LX' WHEN '7' THEN 'LXX' WHEN '8' THEN 'LXXX' WHEN '9' THEN 'XC' ELSE '' END

    +CASE substring(@c,10,1) WHEN '1' THEN 'I' WHEN '2' THEN 'II' WHEN '3' THEN 'III' WHEN '4' THEN 'IV' WHEN '5' THEN 'V' WHEN '6' THEN 'VI' WHEN '7' THEN 'VII' WHEN '8' THEN 'VIII' WHEN '9' THEN 'IX' ELSE '' END

    ;

  • CELKO (12/9/2014)


    Why not use a look-up table? What you posted is a FORTRAN or BASIC program written in T-SQL procedural code.

    I agree.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply