Technical Article

Generate Roman Numerals

,

This function is used to generate roman numerals for given integer.

Check this out....

select dbo.fn_get_roman_numerals(133,1)

select dbo.fn_get_roman_numerals(133,0)

Regards,

Vignesh Arulmani

create function fn_get_roman_numerals(@inputstring as int,@isupper bit)
returns varchar(128)
as
begin
  declare @returnstring varchar(100), @sub varchar(100),
          @number bigint, @position int, @mod bigint
  set @returnstring = ''
  
  --set roman symbols orders
set @sub = 'IVXLCDM' 

  if @inputstring=0
  begin
     set @returnstring = ''
  end
  else
  begin
     select @position = 1, @number = abs(@inputstring)
     while(@position<=5)
     begin
       set @mod = @number % 10
       set @number = @number / 10
       select @returnstring = case
         when @mod in (0,1,2,3) then
           replicate(substring(@sub,@position,1),@mod) + @returnstring
         when @mod in (4) then
           substring(@sub,@position,2) + @returnstring
         when @mod in (5,6,7,8) then
           substring(@sub,@position+1,1) + replicate(substring(@sub,@position,1),@mod-5) + @returnstring
         when @mod in (9) then
           substring(@sub,@position,1) + substring(@sub,@position+2,1) + @returnstring
         end
       set @position = @position + 2
     end
     set @returnstring = replicate('M',@number) + @returnstring
     if @inputstring < 0
     set @returnstring = '-' + @returnstring
   end

   if @isupper = 0
set @returnstring = lower(@returnstring)

   return @returnstring
end
go


select dbo.fn_get_roman_numerals(133,1)
select dbo.fn_get_roman_numerals(133,0)

Rate

5 (4)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (4)

You rated this post out of 5. Change rating