CREATE FUNCTION NumToWords_Ver2(@num numeric)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @cNum varchar(20)
SET @cNum = @num
DECLARE @len int
SET @len = LEN(@num)
DECLARE @place int
SET @place = 0
DECLARE @digit varchar(1), @tens varchar(2)
DECLARE @res varchar(1000)
DECLARE @nullStr int
SET @res = ''
DECLARE @tblNum TABLE(Num int, NumStr varchar(20))
INSERT INTO @tblNum
SELECT 1, ' One' UNION
SELECT 2, ' Two' UNION
SELECT 3, ' Three' UNION
SELECT 4, ' Four' UNION
SELECT 5, ' Five' UNION
SELECT 6, ' Six' UNION
SELECT 7, ' Seven' UNION
SELECT 8, ' Eight' UNION
SELECT 9, ' Nine' UNION
SELECT 10, ' Ten' UNION
SELECT 11, ' Eleven' UNION
SELECT 12, ' Twelve' UNION
SELECT 13, ' Thirteen' UNION
SELECT 14, ' Fourteen' UNION
SELECT 15, ' Fifteen' UNION
SELECT 16, ' Sixteen' UNION
SELECT 17, ' Seventeen' UNION
SELECT 18, ' Eighteen' UNION
SELECT 19, ' Nineteen' UNION
SELECT 20, ' Twenty' UNION
SELECT 30, ' Thirty' UNION
SELECT 40, ' Fourty' UNION
SELECT 50, ' Fifty' UNION
SELECT 60, ' Sixty' UNION
SELECT 70, ' Seventy' UNION
SELECT 80, ' Eighty' UNION
SELECT 90, ' Ninety'
DECLARE @hundred varchar(200)
SET @hundred = ''
DECLARE @nStr varchar(20)
SET @place = @len
WHILE @place > 0
BEGIN
SET @place = @place - 1
SET @nStr = NULL
SET @digit = SUBSTRING(@cNum, @len-@place, 1)
IF (@place+1) % 3 = 1 --One's place
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
IF @nStr IS NOT NULL
SET @hundred = @nStr
IF LEN(@hundred) > 0
SET @res = @res + @hundred + CASE @place / 3
WHEN 0 THEN ''
WHEN 1 THEN ' Thousand'
WHEN 2 THEN ' Million'
WHEN 3 THEN ' Billion'
WHEN 4 THEN ' Trillion'
WHEN 5 THEN ' Qwadrillion'
WHEN 6 THEN ' Quintillion'
END
SET @hundred = ''
END
IF (@place+1) % 3 = 0 --Hundred's place
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
IF @nStr IS NOT NULL
SET @hundred = @nStr + ' Hundred'
SET @tens = SUBSTRING(@cNum, @len-@place+1, 2)
IF LEN(@hundred) > 0 AND (@tens = '' OR @tens = '00')
SET @res = @res + @hundred + CASE (@place+1) / 3
WHEN 0 THEN ''
WHEN 1 THEN ''
WHEN 2 THEN ' Thousand'
WHEN 3 THEN ' Million'
WHEN 4 THEN ' Billion'
WHEN 5 THEN ' Trillion'
WHEN 6 THEN ' Qwadrillion'
WHEN 7 THEN ' Quintillion'
END
ELSE
SET @res = @res + @hundred
SET @hundred = ''
END
ELSE IF (@place+1) % 3 = 2 --Ten's place
BEGIN
SET @tens = SUBSTRING(@cNum, @len-@place, 2)
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @tens
IF @nStr IS NULL
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit * 10
SET @digit = SUBSTRING(@cNum, @len-@place+1, 1)
SELECT @nStr = @nStr + NumStr FROM @tblNum WHERE Num = @digit
END
SET @hundred = @nStr
IF LEN(@hundred) > 0
SET @res = @res + @hundred + CASE (@place+1) / 3
WHEN 0 THEN ''
WHEN 1 THEN ' Thousand'
WHEN 2 THEN ' Million'
WHEN 3 THEN ' Billion'
WHEN 4 THEN ' Trillion'
WHEN 5 THEN ' Qwadrillion'
WHEN 6 THEN ' Quintillion'
END
SET @place = @place - 1
SET @hundred = ''
END
END
RETURN @res
END
RETURNS varchar(1000)
AS
BEGIN
DECLARE @cNum varchar(20)
SET @cNum = @num
DECLARE @len int
SET @len = LEN(@num)
DECLARE @place int
SET @place = 0
DECLARE @digit varchar(1), @tens varchar(2)
DECLARE @res varchar(1000)
DECLARE @nullStr int
SET @res = ''
DECLARE @tblNum TABLE(Num int, NumStr varchar(20))
INSERT INTO @tblNum
SELECT 1, ' One' UNION
SELECT 2, ' Two' UNION
SELECT 3, ' Three' UNION
SELECT 4, ' Four' UNION
SELECT 5, ' Five' UNION
SELECT 6, ' Six' UNION
SELECT 7, ' Seven' UNION
SELECT 8, ' Eight' UNION
SELECT 9, ' Nine' UNION
SELECT 10, ' Ten' UNION
SELECT 11, ' Eleven' UNION
SELECT 12, ' Twelve' UNION
SELECT 13, ' Thirteen' UNION
SELECT 14, ' Fourteen' UNION
SELECT 15, ' Fifteen' UNION
SELECT 16, ' Sixteen' UNION
SELECT 17, ' Seventeen' UNION
SELECT 18, ' Eighteen' UNION
SELECT 19, ' Nineteen' UNION
SELECT 20, ' Twenty' UNION
SELECT 30, ' Thirty' UNION
SELECT 40, ' Fourty' UNION
SELECT 50, ' Fifty' UNION
SELECT 60, ' Sixty' UNION
SELECT 70, ' Seventy' UNION
SELECT 80, ' Eighty' UNION
SELECT 90, ' Ninety'
DECLARE @hundred varchar(200)
SET @hundred = ''
DECLARE @nStr varchar(20)
SET @place = @len
WHILE @place > 0
BEGIN
SET @place = @place - 1
SET @nStr = NULL
SET @digit = SUBSTRING(@cNum, @len-@place, 1)
IF (@place+1) % 3 = 1 --One's place
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
IF @nStr IS NOT NULL
SET @hundred = @nStr
IF LEN(@hundred) > 0
SET @res = @res + @hundred + CASE @place / 3
WHEN 0 THEN ''
WHEN 1 THEN ' Thousand'
WHEN 2 THEN ' Million'
WHEN 3 THEN ' Billion'
WHEN 4 THEN ' Trillion'
WHEN 5 THEN ' Qwadrillion'
WHEN 6 THEN ' Quintillion'
END
SET @hundred = ''
END
IF (@place+1) % 3 = 0 --Hundred's place
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
IF @nStr IS NOT NULL
SET @hundred = @nStr + ' Hundred'
SET @tens = SUBSTRING(@cNum, @len-@place+1, 2)
IF LEN(@hundred) > 0 AND (@tens = '' OR @tens = '00')
SET @res = @res + @hundred + CASE (@place+1) / 3
WHEN 0 THEN ''
WHEN 1 THEN ''
WHEN 2 THEN ' Thousand'
WHEN 3 THEN ' Million'
WHEN 4 THEN ' Billion'
WHEN 5 THEN ' Trillion'
WHEN 6 THEN ' Qwadrillion'
WHEN 7 THEN ' Quintillion'
END
ELSE
SET @res = @res + @hundred
SET @hundred = ''
END
ELSE IF (@place+1) % 3 = 2 --Ten's place
BEGIN
SET @tens = SUBSTRING(@cNum, @len-@place, 2)
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @tens
IF @nStr IS NULL
BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit * 10
SET @digit = SUBSTRING(@cNum, @len-@place+1, 1)
SELECT @nStr = @nStr + NumStr FROM @tblNum WHERE Num = @digit
END
SET @hundred = @nStr
IF LEN(@hundred) > 0
SET @res = @res + @hundred + CASE (@place+1) / 3
WHEN 0 THEN ''
WHEN 1 THEN ' Thousand'
WHEN 2 THEN ' Million'
WHEN 3 THEN ' Billion'
WHEN 4 THEN ' Trillion'
WHEN 5 THEN ' Qwadrillion'
WHEN 6 THEN ' Quintillion'
END
SET @place = @place - 1
SET @hundred = ''
END
END
RETURN @res
END
No comments:
Post a Comment