sql server function to mysql with same logic -
the below sql server function converted mysql in mysql substring working in different manner when compared sql server can make execute in mysql same logic?
alter function [dbo].[udf_num_towords] ( @number numeric (38, 0) -- input number many 18 digits ) returns varchar(8000) begin declare @inputnumber varchar(38) declare @numberstable table (number char(2), word varchar(10)) declare @outputstring varchar(8000) declare @length int declare @counter int declare @loops int declare @position int declare @chunk char(3) -- chunks of 3 numbers declare @tensones char(2) declare @hundreds char(1) declare @tens char(1) declare @ones char(1) if @number = 0 return 'zero' -- initialize variables select @inputnumber = convert(varchar(38), @number) , @outputstring = '' , @counter = 1 select @length = len(@inputnumber) , @position = len(@inputnumber) - 2 , @loops = len(@inputnumber)/3 -- make sure there loop added remaining numbers if len(@inputnumber) % 3 <> 0 set @loops = @loops + 1 -- insert data numbers , words insert @numberstable select '00', '' union select '01', 'one' union select '02', 'two' union select '03', 'three' union select '04', 'four' union select '05', 'five' union select '06', 'six' union select '07', 'seven' union select '08', 'eight' union select '09', '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', 'forty' union select '50', 'fifty' union select '60', 'sixty' union select '70', 'seventy' union select '80', 'eighty' union select '90', 'ninety' while @counter <= @loops begin -- chunks of 3 numbers @ time, padded 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 word directly @numberstable if convert(int, @tensones) <= 20 or @ones='0' begin set @outputstring = (select word @numberstable @tensones = number) + case @counter when 1 '' -- no name when 2 ' thousand ' when 3 ' million ' when 4 ' billion ' when 5 ' trillion ' when 6 ' quadrillion ' when 7 ' quintillion ' when 8 ' sextillion ' when 9 ' septillion ' when 10 ' octillion ' when 11 ' nonillion ' when 12 ' decillion ' when 13 ' undecillion ' else '' end + @outputstring end else begin -- break down ones , tens separately set @outputstring = ' ' + (select word @numberstable @tens + '0' = number) + '-' + (select word @numberstable '0'+ @ones = number) + case @counter when 1 '' -- no name when 2 ' thousand ' when 3 ' million ' when 4 ' billion ' when 5 ' trillion ' when 6 ' quadrillion ' when 7 ' quintillion ' when 8 ' sextillion ' when 9 ' septillion ' when 10 ' octillion ' when 11 ' nonillion ' when 12 ' decillion ' when 13 ' undecillion ' else '' end + @outputstring end -- hundreds if @hundreds <> '0' begin set @outputstring = (select word @numberstable '0' + @hundreds = number) + ' hundred ' + @outputstring end end select @counter = @counter + 1 , @position = @position - 3 end -- remove double spaces set @outputstring = ltrim(rtrim(replace(@outputstring, ' ', ' '))) set @outputstring = upper(left(@outputstring, 1)) + substring(@outputstring, 2, 8000) return @outputstring -- return result end
Comments
Post a Comment