Important alert: (current site time 7/15/2013 7:25:22 PM EDT)
 

VB icon

Arabic To Roman

Email
Submitted on: 5/1/2007 3:22:56 PM
By: barkın ünüulu  
Level: Advanced
User Rating: Unrated
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 7123
author picture
(About the author)
 
     The purpose of this short function snippets is to convert the arabic numerals into Roman Numerals in SQL Server Database Management System.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Arabic To Roman
-- Description:The purpose of this short function snippets is to convert the arabic numerals into Roman Numerals in SQL Server Database Management System.
-- By: barkın ünüulu
--
-- Inputs:In the code snippet you are going to recognize 5 user defined functions. The functions which are called One_To_Nine,Tens,Hundreds and Thousands convert the arabic decimals into roman numerals respectively. And at the end the ToRoman function calls the functions according to the length of the arabic numeral entered by the user.
--
-- Returns:The ToRoman function will return a text value, in the terms of database types varchar(20).
--
-- Side Effects:The ToRoman function will only return numbers which are smaller or equal to 4999. As I can not define the character V that has a line on top of it, I have to limit the function.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1147&lngWId=5--for details.--**************************************

CREATE function [dbo].[ToRoman](@Number varchar(10)) returns varchar(20)
 begin
declare @Result varchar(20)
 
 if len(@number)=1
begin
set @Result=dbo.One_To_Nine(@number)
end
 if len(@number)=2
begin
set @Result=dbo.Tens(@number)
end
 if len(@number)=3
begin
set @Result=dbo.Hundreds(@number)
end
if len(@number)=4
begin
set @Result=dbo.Thousands(@number)
end
 
 return @Result
end
CREATE function [dbo].[One_To_Nine](@number varchar(10)) 
returns varchar(10)
begin
 declare @Result varchar(20)
 declare @Ones varchar(1) 
 /*set @KidemNo=left(@KidemNo,dbo.Çizgi_Bul(@KidemNo))*/
 set @Ones=right(@number,1)
if @Ones='1' 
begin
 set @Result='I'
end 
 else if @Ones='2' 
 begin
 set @Result='II'
end 
 else if @Ones='3' 
 begin
 set @Result='III'
end 
 else if @Ones='4' 
 begin
 set @Result='IV'
end 
 else if @Ones='5' 
 begin
 set @Result='V'
end 
else if @Ones='6' 
 begin
 set @Result='VI'
end 
			 else if @Ones='6' 
 begin
 set @Result='VI'
end 
				else if @Ones='7' 
 begin
set@Result='VII'
end 
 else if @Ones='8' 
 begin
 set @Result='VIII'
end 
else if @Ones='9' 
 begin
 set @Result='IX'
end 
 else if @Ones='0' 
 begin
 set @Result=''
end 
 return @Result
 end
CREATE function [dbo].[Tens](@number varchar(10)) returns varchar(10)
begin
 declare @Result varchar(20)
 declare @Tens varchar(10)
 declare @number1 varchar(10)
 /*set @number1=left(@number,dbo.Çizgi_Bul(@number))*/
 set @Tens=left(right(@number,2),1)
if @Tens='1'
 begin
 set @Result='X' +dbo.One_To_Nine(@number)
 end
 else if @Tens='2'
 begin
 set @Result='XX' +dbo.One_To_Nine(@number)
 end
 else if @Tens='3'
 begin
 set @Result='XXX' +dbo.One_To_Nine(@number)
 end
 else if @Tens='4'
 begin
 set @Result='XL' +dbo.One_To_Nine(@number)
 end
else if @Tens='2'
 begin
 set @Result='XX' +dbo.One_To_Nine(@number)
 end
 else if @Tens='5'
 begin
 set @Result='L' +dbo.One_To_Nine(@number)
 end
 else if @Tens='6'
 begin
 set @Result='LX' +dbo.One_To_Nine(@number)
 end
			else if @Tens='7'
 begin
 set @Result='LXX' +dbo.One_To_Nine(@number)
 end
else if @Tens='8'
 begin
 set @Result='LXXX' +dbo.One_To_Nine(@number)
 end
else if @Tens='9'
 begin
 set @Result='XC' +dbo.One_To_Nine(@number)
 end 
ELSE if @Tens='0'
begin
 set @Result=''+ dbo.One_To_Nine(@number)
end 
 return @Result
end
CREATE function [dbo].[Hundreds](@number varchar(10)) returns varchar(10)
 begin
declare @Result varchar(20)
declare @Hundreds varchar(10)
declare @number1 varchar(10)
/*set @number1=left(@number,dbo.Çizgi_Bul(@number))*/
set @Hundreds=left(right(@number,3),1)
 if @Hundreds='1'
begin
set @Result='C'+ dbo.Tens(@number)
end
if @Hundreds='2'
begin
set @Result='CC'+ dbo.Tens(@number)
end
 if @Hundreds='3'
begin
set @Result='CCC'+ dbo.Tens(@number)
end
if @Hundreds='4'
begin
set @Result='CD'+ dbo.Tens(@number)
end
if @Hundreds='5'
begin
set @Result='D'+ dbo.Tens(@number)
end
if @Hundreds='6'
begin
set @Result='CD'+ dbo.Tens(@number)
end
if @Hundreds='7'
begin
set @Result='DCC'+ dbo.Tens(@number)
end
 if @Hundreds='8'
begin
set @Result='DCCC'+ dbo.Tens(@number)
end
if @Hundreds='9'
begin
set @Result='CM'+ dbo.Tens(@number)
end
if @Hundreds='0'
 begin
set @Result=''+dbo.Tens(@number)
 end 
Return @Result
end 
CREATE function [dbo].[Thousands] (@number varchar(10)) returns varchar(10)
begin 
 declare @Result varchar(20)
 declare @number1 varchar(10)
 declare @Thousands varchar(10)
 /*set @number1=left(@number,dbo.Çizgi_Bul(@number))*/
		 set @Thousands =left(@number,1)
if @Thousands='1'
begin
 set @Result='M' + dbo.Hundreds(@number)
end
 else if @Thousands ='2'
begin
 set @Result='MM'+dbo.Hundreds(@number)
end
else if @Thousands ='3'
begin
 set @Result='MMM'+dbo.Hundreds(@number)
end
else if @Thousands ='4'
begin
 set @Result='MMMM'+dbo.Hundreds(@number)
end
 return @Result
end 


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/8/2007 12:51:10 AMJeff Moden

5 functions to do one? Try this, instead...

CREATE FUNCTION dbo.ArabicToRoman (@AVal INT)
RETURNS VARCHAR(16)
AS
BEGIN

IF @AVal < 1 OR @AVal > 4999 RETURN '** ERROR **'

DECLARE
@TenE0 INT,
@TenE1 INT,
@TenE2 INT,
@TenE3 INT

SELECT
@TenE0 = @AVal%10,
@TenE1 = @AVal%100/10,
@TenE2 = @AVal%1000/100,
@TenE3 = @AVal%10000/1000

RETURN
(SELECT REPLICATE('M',@TenE3)
+ CASE
WHEN @TenE2 <4 THEN REPLICATE('C',@TenE2)
WHEN @TenE2 =4 THEN 'CD'
WHEN @TenE2 =9 THEN 'CM'
WHEN @TenE2 >=5 THEN 'D'+REPLICATE('C',@TenE2-5)
ELSE ''
END
+ CASE
WHEN @TenE1 <4 THEN REPLICATE('X',@TenE1)
WHEN @TenE1 =4 THEN 'XL'
WHEN @TenE1 =9 THEN 'XC'
WHEN @TenE1 >=5 THEN 'L'+REPLICATE('X',@TenE1-5)
ELSE ''
END
+ CASE
WHEN @TenE0 <4 THEN REPLICATE('I',@TenE0)
WHEN @TenE0 =4 THEN 'IV'
WHEN @TenE0 =9 THEN 'IX'
WHEN @TenE0 >=5 THEN 'V'+REPLICATE('I',@TenE0-5)
ELSE ''
END
)
END

(If this comment was disrespectful, please report it.)

 
5/8/2007 9:25:19 AMJeff Moden

Uh, sorry about the way that last comment came out... not much formatting ability available on these responses.
(If this comment was disrespectful, please report it.)

 
3/31/2009 2:28:52 PMsanket

thank you this code really helped me
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.