Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
surasrinivasarao
Starting Member
2 Posts |
Posted - 2008-06-06 : 01:21:18
|
| hi i need a procedure that converts amount in numer to wordsRegrdssrinivas |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-06 : 01:34:07
|
| amount in number to words? you mean like 1000 to One Thousand etc? |
 |
|
|
surasrinivasarao
Starting Member
2 Posts |
Posted - 2008-06-06 : 03:34:53
|
| yes if we have this number 142541 result should be one lac fourty two thousand five fourty one |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-06 : 03:48:13
|
quote: Originally posted by surasrinivasarao yes if we have this number 142541 result should be one lac fourty two thousand five fourty one
Thats not possible unless you hardcode them. i think it will be more easier if you need result like one four two five four one |
 |
|
|
Johnho008
Starting Member
24 Posts |
Posted - 2008-06-06 : 07:17:06
|
| not done the full code but guess you could do something like thisDeclare @amount int, @one varchar(10), @two varchar(10), @three varchar(10), @WordAmount varchar(1000), @ten varchar(10), @twenty varchar(10), @thirty varchar(10), @l int, @word varchar(100)set @one= 'one'set @two='two'set @three='three'set @ten ='ten'set @twenty ='twenty'set @thirty ='thirty'set @amount ='432'set @l=0WHILE @l <> (SELECT LEN(@amount)) BEGIN SET @word= (SELECT CASE WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '1' THEN 'ONE' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '2' THEN 'TWO' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '3' THEN 'THREE' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '4' THEN 'FOUR' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '5' THEN 'FIVE' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '6' THEN 'SIX' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '7' THEN 'SEVEN' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '8' THEN 'EIGHT' WHEN SUBSTRING(CAST(@amount as varchar(10)), (SELECT LEN(@amount)-@l), 1) = '9' THEN 'NINE' END) SET @wordamount= (SELECT CASE WHEN @l=0 THEN @word WHEN @l=1 THEN @word +' ' +@wordamount WHEN @l=2 THEN @word +' HUNDRED ' +@wordamount WHEN @l=3 THEN @word +' THOUSAND ' +@wordamount END)--'1532' SET @l=@l+1END Print @wordamount |
 |
|
|
mosid
Starting Member
1 Post |
Posted - 2011-11-02 : 02:44:24
|
| Use my function I created compatible with SQL 2005 and later/*SAMPLE CALL: SELECT dbo.NumberToWords(-56111310485455007101062400850945120523)*/CREATE FUNCTION dbo.NumberToWords(@Number NUMERIC(38,0))RETURNS VARCHAR(8000)ASBEGIN IF @Number=0 RETURN 'zero' DECLARE @a SMALLINT, @Words VARCHAR(8000)='' IF @Number<0 SELECT @Words='negative ', @Number=@Number*-1 DECLARE @num TABLE (num NUMERIC(38,0), word varchar(15)) INSERT INTO @num VALUES (1000000000000000000000000000000000000, 'undecillion '), (1000000000000000000000000000000000, 'decillion '), (1000000000000000000000000000000, 'nonillion '), (1000000000000000000000000000, 'octillion '), (1000000000000000000000000, 'septillion '), (1000000000000000000000, 'sextillion '),(1000000000000000000, 'quintillion '), (1000000000000000, 'quadrillion '),(1000000000000, 'trillion '), (1000000000, 'billion '),(1000000, 'million '),(1000, 'thousand '),(100, 'hundred '), (90, 'ninety '),(80, 'eighty '),(70, 'seventy '),(60, 'sixty '), (50, 'fifty '),(40, 'forty '),(30, 'thirty '),(20, 'twenty '), (19, 'nineteen '),(18, 'eighteen '),(17, 'seventeen '),(16, 'sixteen '), (15, 'fifteen '),(14, 'fourteen '),(13, 'thirteen '),(12, 'twelve '),(11, 'eleven '), (10, 'ten '),(9, 'nine '),(8, 'eight '),(7, 'seven '),(6, 'six '), (5, 'five '),(4, 'four '),(3, 'three '),(2, 'two '),(1, 'one ') -- search for the highest matched number first WHILE @Number > 0 SELECT TOP 1 @a=@Number/num -- get how many hundreds, thousands, millions, billions etc. ,@Words=@Words + CASE WHEN @Number>100 THEN dbo.NumberToWords(@a) ELSE '' END + word ,@Number=@Number - num * @a -- get the remaining number FROM @num WHERE num <= @Number RETURN @WordsEND |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-02 : 05:29:57
|
| reopened a older thread?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2011-11-02 : 12:18:59
|
Here is a function I use that works very well for Money amounts, but can easily be converted to what you need.set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo--select dbo.udf_Num_ToWords(42.8572)ALTER FUNCTION [dbo].[udf_Num_ToWords] ( @Input Numeric(32,3)-- Input number with as many as 18 digits) RETURNS VARCHAR(8000) /** Converts a integer number as large as 34 digits into the * equivalent words. The first letter is capitalized.** Attribution: Based on NumberToWords by Srinivas Sampath* as revised by Nick Barclay** Example:select dbo.udf_Num_ToWords (1234567890) + CHAR(10) + dbo.udf_Num_ToWords (0) + CHAR(10) + dbo.udf_Num_ToWords (123) + CHAR(10)select dbo.udf_Num_ToWords(76543210987654321098765432109876543210) DECLARE @i numeric (38,0)SET @i = 0WHILE @I <= 1000 BEGIN PRINT convert (char(5), @i) + convert(varchar(255), dbo.udf_Num_ToWords(@i)) SET @I = @i + 1 END** Published as the T-SQL UDF of the Week Vol 2 #9 2/17/03****************************************************************/AS BEGIN--declare @Input Numeric(32,3) --set @Input = 42.8572Declare @Number Numeric(32)set @Number = floor(@Input)Declare @Cents as int--Select cast(@Input as money)set @Cents = 100*(cast(@Input as decimal(32,2))- @Number)DECLARE @inputNumber VARCHAR(38)DECLARE @NumbersTable TABLE (number CHAR(2), word VARCHAR(10))DECLARE @outputString VARCHAR(8000)DECLARE @length INTDECLARE @counter INTDECLARE @loops INTDECLARE @position INTDECLARE @chunk CHAR(3) -- for chunks of 3 numbersDECLARE @tensones CHAR(2)DECLARE @hundreds CHAR(1)DECLARE @tens CHAR(1)DECLARE @ones CHAR(1)IF @Number = 0 Return 'Zero'-- initialize the variablesSELECT @inputNumber = CONVERT(varchar(38), @Number) , @outputString = '' , @counter = 1SELECT @length = LEN(@inputNumber) , @position = LEN(@inputNumber) - 2 , @loops = LEN(@inputNumber)/3-- make sure there is an extra loop added for the remaining numbersIF LEN(@inputNumber) % 3 <> 0 SET @loops = @loops + 1-- insert data for the numbers and wordsINSERT INTO @NumbersTable SELECT '00', '' UNION ALL SELECT '01', 'one' UNION ALL SELECT '02', 'two' UNION ALL SELECT '03', 'three' UNION ALL SELECT '04', 'four' UNION ALL SELECT '05', 'five' UNION ALL SELECT '06', 'six' UNION ALL SELECT '07', 'seven' UNION ALL SELECT '08', 'eight' UNION ALL SELECT '09', 'nine' UNION ALL SELECT '10', 'ten' UNION ALL SELECT '11', 'eleven' UNION ALL SELECT '12', 'twelve' UNION ALL SELECT '13', 'thirteen' UNION ALL SELECT '14', 'fourteen' UNION ALL SELECT '15', 'fifteen' UNION ALL SELECT '16', 'sixteen' UNION ALL SELECT '17', 'seventeen' UNION ALL SELECT '18', 'eighteen' UNION ALL SELECT '19', 'nineteen' UNION ALL SELECT '20', 'twenty' UNION ALL SELECT '30', 'thirty' UNION ALL SELECT '40', 'forty' UNION ALL SELECT '50', 'fifty' UNION ALL SELECT '60', 'sixty' UNION ALL SELECT '70', 'seventy' UNION ALL SELECT '80', 'eighty' UNION ALL SELECT '90', 'ninety' WHILE @counter <= @loops BEGIN -- get chunks of 3 numbers at a time, padded with leading zeros SET @chunk = RIGHT('000' + SUBSTRING(@inputNumber, @position, 3), 3) IF @chunk <> '000' BEGIN SELECT @tensones = SUBSTRING(@chunk, 2, 2) , @hundreds = SUBSTRING(@chunk, 1, 1) , @tens = SUBSTRING(@chunk, 2, 1) , @ones = SUBSTRING(@chunk, 3, 1) -- If twenty or less, use the word directly from @NumbersTable IF CONVERT(INT, @tensones) <= 20 OR @Ones='0' BEGIN SET @outputString = (SELECT word FROM @NumbersTable WHERE @tensones = number) + CASE @counter WHEN 1 THEN '' -- No name WHEN 2 THEN ' thousand ' WHEN 3 THEN ' million ' WHEN 4 THEN ' billion ' WHEN 5 THEN ' trillion ' WHEN 6 THEN ' quadrillion ' WHEN 7 THEN ' quintillion ' WHEN 8 THEN ' sextillion ' WHEN 9 THEN ' septillion ' WHEN 10 THEN ' octillion ' WHEN 11 THEN ' nonillion ' WHEN 12 THEN ' decillion ' WHEN 13 THEN ' undecillion ' ELSE '' END + @outputString END ELSE BEGIN -- break down the ones and the tens separately SET @outputString = ' ' + (SELECT word FROM @NumbersTable WHERE @tens + '0' = number) + '-' + (SELECT word FROM @NumbersTable WHERE '0'+ @ones = number) + CASE @counter WHEN 1 THEN '' -- No name WHEN 2 THEN ' thousand ' WHEN 3 THEN ' million ' WHEN 4 THEN ' billion ' WHEN 5 THEN ' trillion ' WHEN 6 THEN ' quadrillion ' WHEN 7 THEN ' quintillion ' WHEN 8 THEN ' sextillion ' WHEN 9 THEN ' septillion ' WHEN 10 THEN ' octillion ' WHEN 11 THEN ' nonillion ' WHEN 12 THEN ' decillion ' WHEN 13 THEN ' undecillion ' ELSE '' END + @outputString END -- now get the hundreds IF @hundreds <> '0' BEGIN SET @outputString = (SELECT word FROM @NumbersTable WHERE '0' + @hundreds = number) + ' hundred ' + @outputString END END SELECT @counter = @counter + 1 , @position = @position - 3END-- Remove any double spacesSET @outputString = LTRIM(RTRIM(REPLACE(@outputString, ' ', ' ')))SET @outputstring = UPPER(LEFT(@outputstring, 1)) + SUBSTRING(@outputstring, 2, 8000)Declare @VCents varchar(2)set @VCents =convert(Varchar(20),@Cents)if len(@VCents) = 1begin set @VCents = '0' + @VcentsendRETURN UPPER(@outputString) + ' DOLLARS & ' + @VCents + '/100 CENTS'-- return the resultEND Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
|
|
|
|
|