drop function number_to_words;
DELIMITER $$
CREATE FUNCTION `number_to_words`(n INT) RETURNS varchar(100)
BEGIN
-- This function returns the string representation of a number.
-- Took it from StackOverflow and Improved to return up to 8 digits in Indian Numbering system
-- Upto: 9,99,99,999/-
-- which is: "Nine Crore Ninety Nine Lakh Ninety Nine Thousand Nine Hundred Ninety Nine"
-- it can be extended further.
-- The idea is:
-- For each digit you need a position,
-- For each position, you assign a string
declare ans varchar(100);
declare dig1, dig2, dig3, dig4, dig5, dig6, dig7, dig8 int;
set ans = '';
set dig8 = CAST(RIGHT(CAST(floor(n / 10000000) as CHAR(8)), 1) as SIGNED);
set dig7 = CAST(RIGHT(CAST(floor(n / 1000000) as CHAR(8)), 1) as SIGNED);
set dig6 = CAST(RIGHT(CAST(floor(n / 100000) as CHAR(8)), 1) as SIGNED);
set dig5 = CAST(RIGHT(CAST(floor(n / 10000) as CHAR(8)), 1) as SIGNED);
set dig4 = CAST(RIGHT(CAST(floor(n / 1000) as CHAR(8)), 1) as SIGNED);
set dig3 = CAST(RIGHT(CAST(floor(n / 100) as CHAR(8)), 1) as SIGNED);
set dig2 = CAST(RIGHT(CAST(floor(n / 10) as CHAR(8)), 1) as SIGNED);
set dig1 = CAST(RIGHT(floor(n), 1) as SIGNED);
if dig8 > 0 then
case
when dig8=1 then set ans=concat(ans, ' one crore ');
when dig8=2 then set ans=concat(ans, ' two crore ');
when dig8=3 then set ans=concat(ans, ' three crore ');
when dig8=4 then set ans=concat(ans, ' four crore ');
when dig8=5 then set ans=concat(ans, ' five crore ');
when dig8=6 then set ans=concat(ans, ' six crore ');
when dig8=7 then set ans=concat(ans, ' seven crore ');
when dig8=8 then set ans=concat(ans, ' eight crore ');
when dig8=9 then set ans=concat(ans, ' nine crore ');
else set ans = ans;
end case;
end if;
if dig7 = 1 then
case
when (dig7*10 + dig6)=10 then set ans=concat(ans, ' ten lakh ');
when (dig7*10 + dig6)=11 then set ans=concat(ans, ' eleven lakh ');
when (dig7*10 + dig6)=12 then set ans=concat(ans, ' twelve lakh ');
when (dig7*10 + dig6)=13 then set ans=concat(ans, ' thirteen lakh ');
when (dig7*10 + dig6)=14 then set ans=concat(ans, ' fourteen lakh ');
when (dig7*10 + dig6)=15 then set ans=concat(ans, ' fifteen lakh ');
when (dig7*10 + dig6)=16 then set ans=concat(ans, ' sixteen lakh ');
when (dig7*10 + dig6)=17 then set ans=concat(ans, ' seventeenlakh ');
when (dig7*10 + dig6)=18 then set ans=concat(ans, ' eighteen lakh ');
when (dig7*10 + dig6)=19 then set ans=concat(ans, ' nineteen lakh ');
else set ans = ans;
end case;
elseif dig7 > 0 then
case
when dig7=2 then set ans=concat(ans, ' twenty ');
when dig7=3 then set ans=concat(ans, ' thirty ');
when dig7=4 then set ans=concat(ans, ' fourty ');
when dig7=5 then set ans=concat(ans, ' fifty ');
when dig7=6 then set ans=concat(ans, ' sixty ');
when dig7=7 then set ans=concat(ans, ' seventy ');
when dig7=8 then set ans=concat(ans, ' eighty ');
when dig7=9 then set ans=concat(ans, ' ninety ');
else set ans=ans;
end case;
end if;
if dig6 > 0 then
case
when dig6=1 then set ans=concat(ans, 'one lakh');
when dig6=2 then set ans=concat(ans, 'two lakh');
when dig6=3 then set ans=concat(ans, 'three lakh');
when dig6=4 then set ans=concat(ans, 'four lakh');
when dig6=5 then set ans=concat(ans, 'five lakh');
when dig6=6 then set ans=concat(ans, 'six lakh');
when dig6=7 then set ans=concat(ans, 'seven lakh');
when dig6=8 then set ans=concat(ans, 'eight lakh');
when dig6=9 then set ans=concat(ans, 'nine lakh');
else set ans = ans;
end case;
end if;
if dig5 = 1 then
case
when (dig5*10 + dig4) = 10 then set ans=concat(ans, ' ten thousand ');
when (dig5*10 + dig4) = 11 then set ans=concat(ans, ' eleven thousand ');
when (dig5*10 + dig4) = 12 then set ans=concat(ans, ' twelve thousand ');
when (dig5*10 + dig4) = 13 then set ans=concat(ans, ' thirteen thousand ');
when (dig5*10 + dig4) = 14 then set ans=concat(ans, ' fourteen thousand ');
when (dig5*10 + dig4) = 15 then set ans=concat(ans, ' fifteen thousand ');
when (dig5*10 + dig4) = 16 then set ans=concat(ans, ' sixteen thousand ');
when (dig5*10 + dig4) = 17 then set ans=concat(ans, ' seventeen thousand ');
when (dig5*10 + dig4) = 18 then set ans=concat(ans, ' eighteen thousand ');
when (dig5*10 + dig4) = 19 then set ans=concat(ans, ' nineteen thousand ');
else set ans=ans;
end case;
else
if dig5 > 0 then
case
when dig5=2 then set ans=concat(ans, ' twenty');
when dig5=3 then set ans=concat(ans, ' thirty');
when dig5=4 then set ans=concat(ans, ' fourty');
when dig5=5 then set ans=concat(ans, ' fifty');
when dig5=6 then set ans=concat(ans, ' sixty');
when dig5=7 then set ans=concat(ans, ' seventy');
when dig5=8 then set ans=concat(ans, ' eighty');
when dig5=9 then set ans=concat(ans, ' ninety');
else set ans=ans;
end case;
end if;
if dig4 > 0 then
case
when dig4=1 then set ans=concat(ans, ' one thousand ');
when dig4=2 then set ans=concat(ans, ' two thousand ');
when dig4=3 then set ans=concat(ans, ' three thousand ');
when dig4=4 then set ans=concat(ans, ' four thousand ');
when dig4=5 then set ans=concat(ans, ' five thousand ');
when dig4=6 then set ans=concat(ans, ' six thousand ');
when dig4=7 then set ans=concat(ans, ' seven thousand ');
when dig4=8 then set ans=concat(ans, ' eight thousand ');
when dig4=9 then set ans=concat(ans, ' nine thousand ');
else set ans=ans;
end case;
end if;
if dig4 = 0 AND (dig5 != 0 ) then
set ans=concat(ans, ' thousand ');
elseif dig4 = 0 AND (dig6 != 0)||(dig7 != 0)||(dig8 != 0) then
set ans=concat(ans, ' ');
else
set ans=concat(ans);
end if;
end if;
if dig3 > 0 then
case
when dig3=1 then set ans=concat(ans, 'one hundred');
when dig3=2 then set ans=concat(ans, 'two hundred');
when dig3=3 then set ans=concat(ans, 'three hundred');
when dig3=4 then set ans=concat(ans, 'four hundred');
when dig3=5 then set ans=concat(ans, 'five hundred');
when dig3=6 then set ans=concat(ans, 'six hundred');
when dig3=7 then set ans=concat(ans, 'seven hundred');
when dig3=8 then set ans=concat(ans, 'eight hundred');
when dig3=9 then set ans=concat(ans, 'nine hundred');
else set ans = ans;
end case;
end if;
if dig2 = 1 then
case
when (dig2*10 + dig1) = 10 then set ans=concat(ans, ' ten');
when (dig2*10 + dig1) = 11 then set ans=concat(ans, ' eleven');
when (dig2*10 + dig1) = 12 then set ans=concat(ans, ' twelve');
when (dig2*10 + dig1) = 13 then set ans=concat(ans, ' thirteen');
when (dig2*10 + dig1) = 14 then set ans=concat(ans, ' fourteen');
when (dig2*10 + dig1) = 15 then set ans=concat(ans, ' fifteen');
when (dig2*10 + dig1) = 16 then set ans=concat(ans, ' sixteen');
when (dig2*10 + dig1) = 17 then set ans=concat(ans, ' seventeen');
when (dig2*10 + dig1) = 18 then set ans=concat(ans, ' eighteen');
when (dig2*10 + dig1) = 19 then set ans=concat(ans, ' nineteen');
else set ans=ans;
end case;
else
if dig2 > 0 then
case
when dig2=2 then set ans=concat(ans, ' twenty');
when dig2=3 then set ans=concat(ans, ' thirty');
when dig2=4 then set ans=concat(ans, ' fourty');
when dig2=5 then set ans=concat(ans, ' fifty');
when dig2=6 then set ans=concat(ans, ' sixty');
when dig2=7 then set ans=concat(ans, ' seventy');
when dig2=8 then set ans=concat(ans, ' eighty');
when dig2=9 then set ans=concat(ans, ' ninety');
else set ans=ans;
end case;
end if;
if dig1 > 0 then
case
when dig1=1 then set ans=concat(ans, ' one');
when dig1=2 then set ans=concat(ans, ' two');
when dig1=3 then set ans=concat(ans, ' three');
when dig1=4 then set ans=concat(ans, ' four');
when dig1=5 then set ans=concat(ans, ' five');
when dig1=6 then set ans=concat(ans, ' six');
when dig1=7 then set ans=concat(ans, ' seven');
when dig1=8 then set ans=concat(ans, ' eight');
when dig1=9 then set ans=concat(ans, ' nine');
else set ans=ans;
end case;
end if;
end if;
return str_titlecase(trim(ans));
END
$$