تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
طريقة عمل تفقيط للأرقام في Sql
#1
كاتب الموضوع : AhmedEssawy


كود :
CREATE FUNCTION NumToWords
(@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)
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)
WHILE @place < @len

BEGIN
SET @tens = ''
SET @nStr = NULL
SET @digit = SUBSTRING(@cNum, @len - @place, 1)
--SET @digit = SUBSTRING(@Num, @len - @p
-- lace, 1)
IF @place % 3 = 0-- one's place

BEGIN
-- Check ten's place
SET @tens = SUBSTRING(@cNum, @len - @place - 1, 2)
--SET @tens = SUBSTRING(@Num, @len - @pl
-- ace - 1, 2)
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @tens
IF @nStr IS NOT NULL
SELECT @hundred = @hundred + @nStr, @place = @place + 1
ELSE

BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
SET @hundred = ISNULL(@nStr, '') + @hundred
END
END
ELSE IF @place % 3 = 1-- Ten's place

BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit * 10--POWER(10, @place % 3)
SET @hundred = ISNULL(@nStr, '') + @hundred
END
IF @place % 3 = 2-- '000 place

BEGIN
SELECT @nStr = NumStr FROM @tblNum WHERE Num = @digit
IF @nStr IS NOT NULL
SET @hundred = @nStr + ' HUNDRED' + @hundred
--print '000 place'
IF LEN(@hundred) > 0
SET @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 ' QUADRILLION'
WHEN 6 THEN ' QUINTILLION'
END + @res
SET @hundred = ''
END
SET @place = @place + 1
END
RETURN @res
END
}}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  طريقة لاختيار صفوف عشوائية RaggiTech 0 592 17-10-12, 02:07 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم