Convert Number to Roman Letter

CREATE FUNCTION [dbo].[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’)
END

 

Run: SELECT  DBO.fnConvertIntToRoman(999)

Result :CMXCIX

Advertisements

Author: tdmsql

I am a PL-Sql Developer. I am searching some sites sql studies but its bored. i like create studies note my own way. so that i created this site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s