In this post, I am sharing small function to convert Number to Roman Numeral in SQL Server
CREATE FUNCTION fnConvertIntToRoman(@i INT) RETURNS VARCHAR(100) AS BEGIN RETURN Replicate('M', @i/1000) + REPLACE(REPLACE(REPLACE( Replicate('C', @i%1000/100), Replicate('C', 9), 'CM'), Replicate('C', 5), 'D'), Replicate('C', 4), 'CD') + REPLACE(REPLACE(REPLACE( Replicate('X', @i%100 / 10), Replicate('X', 9),'XC'), Replicate('X', 5), 'L'), Replicate('X', 4), 'XL') + REPLACE(REPLACE(REPLACE( Replicate('I', @i%10), Replicate('I', 9),'IX'), Replicate('I', 5), 'V'), Replicate('I', 4),'IV') ENDLet's test the function
select dbo.fnConvertIntToRoman(1) as 'Roman Value' select dbo.fnConvertIntToRoman(15) as 'Roman Value' select dbo.fnConvertIntToRoman(118) as 'Roman Value' select dbo.fnConvertIntToRoman(1008) as 'Roman Value'OUTPUT
2 comments:
Write CommentsVery Usefull.
ReplyDeleteThanx.
its very much required thank you
ReplyDelete