我在mysql中执行查询时尝试按如下方式打印输出。
Name Salary Sal_in_Words
Mohan 45000 Rupees Forty Five Thousand Only
列Salary的值为45000,在第三列中,第二列中的值通过Query转换为单词。
我发现了一些文章,在Oracle中我们可以使用以下查询获得上述结果:
select Salary, (' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.')) Sal_in_Words from employee
输出:
Name Salary Sal_in_Words
Suresh 45000 Rupees Forty Five Thousand Only
在MySQL中,我尝试过LPAD,RPAD但是这些只是在结果中添加字符串而不是将单词转换为字符串。
我找到了一些教程,但所有人都解释了“TO_CHAR(日期)”。
有什么办法吗?
答案 0 :(得分:2)
在MySQL中没有简单的功能你需要在函数的帮助下编写函数来实现这个结果。
检查以下内容..这对我有用.. Reference
DELIMITER $$
CREATE FUNCTION `number_to_string`(n INT) RETURNS varchar(100)
BEGIN
-- This function returns the string representation of a number.
-- It's just an example... I'll restrict it to hundreds, but
-- it can be extended easily.
-- 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 int; -- (one variable per digit)
set ans = '';
set dig3 = floor(n / 100);
set dig2 = floor(n / 10) - dig3*10;
set dig1 = n - (dig3*100 + dig2*10);
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 trim(ans);
END$$
DELIMITER ;
如果收到以下错误..
#1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
运行此查询:
SET GLOBAL log_bin_trust_function_creators = 1;
之后在Mysql中创建一个函数:
调用函数就像下面的命令一样:
SELECT number_to_string( 666 );
您将获得输出:
number_to_string( 666 )
six hundred sixty six
希望这对其他人有帮助!
答案 1 :(得分:1)
FUNCTION `fn_number2words`(_Number double(18,2))
RETURNS varchar(8000)
BEGIN
DECLARE _inputNumber VARCHAR(38);
DECLARE _outputString VARCHAR(8000);
DECLARE _length INT;
DECLARE _counter INT;
DECLARE _loops INT DEFAULT 0;
DECLARE _position INT;
DECLARE _chunk CHAR(3); -- for chunks of 3 numbers
DECLARE _tensones CHAR(2);
DECLARE _hundreds CHAR(1);
DECLARE _tens CHAR(1);
DECLARE _ones CHAR(1);
DECLARE _cents varchar(50);
DECLARE _numtmp1 varchar(50);
DECLARE _numtmp2 varchar(50);
DECLARE _numtmp3 varchar(50);
IF _Number = 0 THEN
SET _outputString = 'Zero';
ELSE
SET _cents = REPLACE(CAST((_Number % 1)*100 as char), '.00', '');
-- initialize the variables
SELECT REPLACE(CAST((_Number - (_Number % 1)) as char), '.00', ''),
'',
1
INTO _inputNumber, _outputString, _counter;
IF MOD(LENGTH(_inputNumber), 3) = 1 THEN
SET _inputNumber = CONCAT('00', _inputNumber);
ELSEIF MOD(LENGTH(_inputNumber), 3) = 2 THEN
SET _inputNumber = CONCAT('0', _inputNumber);
END IF;
SELECT LENGTH(_inputNumber),
(LENGTH(_inputNumber)/3),
LENGTH(_inputNumber)/3
INTO _length, _position, _loops;
WHILE _counter <= _loops DO
SET _chunk = SUBSTR(_inputNumber, ((_position-1)*3)+1, 3);
IF _chunk <> '000' THEN
SELECT
SUBSTR(_chunk, 2, 2), SUBSTR(_chunk, 1, 1), SUBSTR(_chunk, 2, 1), SUBSTR(_chunk, 3, 1)
INTO
_tensones,
_hundreds,
_tens,
_ones;
/*SELECT _inputNumber, _chunk, _tensones, _hundreds, _tens, _ones, _counter, _position, _loops, CAST(_tensones as UNSIGNED INT);*/
IF CAST(_tensones as UNSIGNED) <= 20 OR _ones='0' THEN
SELECT word INTO _numtmp1 FROM tbl_numbers WHERE number = _tensones;
SELECT 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
INTO _numtmp2;
SET _outputString = CONCAT(_numtmp1, _numtmp2, _outputString);
ELSE
SELECT word INTO _numtmp1 FROM tbl_numbers WHERE number = CONCAT(_tens,'0');
SELECT word INTO _numtmp2 FROM tbl_numbers WHERE number = CONCAT('0',_ones);
SELECT 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
INTO _numtmp3;
SET _outputString = CONCAT(' ',_numtmp1, '-',_numtmp2, _numtmp3, _outputString);
END IF;
-- now get the hundreds
IF _hundreds <> '0' THEN
SELECT word INTO _numtmp1 FROM tbl_numbers WHERE number = CONCAT('0',_hundreds);
SET _outputString = CONCAT(_numtmp1, ' hundred ', _outputString);
END IF;
END IF;
/* seed variables */
SELECT (_counter+1), (_position-1)
INTO _counter, _position;
END WHILE;
END IF;
IF RTRIM(LTRIM(_outputString)) = '' THEN
SET _outputString = '';
ELSE
SET _outputString = CONCAT(_outputString, ' PESOS AND ');
END IF;
SET _outputString = UPPER(CONCAT(_outputString,_cents,'/100 CENT(S)')); -- return the result
RETURN _outputString;
END
答案 2 :(得分:1)
我接受了TomPHP的回答并将其扩展到最多6位数:
SELECT number_into_words(999999);
会产生nine hundred ninety nine thousand nine hundred ninety nine
我还更新了dig1
到dig6
的初始设置,因此它对我自己更具可读性(我知道有更有效的方法可以做到这一点)。
DELIMITER $$
CREATE FUNCTION `number_to_words`(n INT) RETURNS varchar(100)
BEGIN
-- This function returns the string representation of a number.
-- It's just an example... I'll restrict it to hundreds, but
-- it can be extended easily.
-- 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 int;
set ans = '';
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 dig6 > 0 then
case
when dig6=1 then set ans=concat(ans, 'one hundred');
when dig6=2 then set ans=concat(ans, 'two hundred');
when dig6=3 then set ans=concat(ans, 'three hundred');
when dig6=4 then set ans=concat(ans, 'four hundred');
when dig6=5 then set ans=concat(ans, 'five hundred');
when dig6=6 then set ans=concat(ans, 'six hundred');
when dig6=7 then set ans=concat(ans, 'seven hundred');
when dig6=8 then set ans=concat(ans, 'eight hundred');
when dig6=9 then set ans=concat(ans, 'nine hundred');
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 || dig6 != 0) then
set ans=concat(ans, ' thousand ');
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 trim(ans);
END
$$
答案 3 :(得分:0)
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `number_to_words`(`n` DECIMAL(10,2)) RETURNS varchar(100) CHARSET latin1
BEGIN
declare ans varchar(100);
declare dig1, dig2, dig3, dig4, dig5, dig6 int;
DECLARE num1,num2 varchar(100);
set ans = '';
IF n != '' THEN // Check is Blank Argument
set num1 = SUBSTRING_INDEX(n, ".", 1);
set num2 = SUBSTRING_INDEX(n, '.', -1);
set dig6 = CAST(RIGHT(CAST(floor(num1 / 100000) as CHAR(8)), 1) as SIGNED);
set dig5 = CAST(RIGHT(CAST(floor(num1 / 10000) as CHAR(8)), 1) as SIGNED);
set dig4 = CAST(RIGHT(CAST(floor(num1 / 1000) as CHAR(8)), 1) as SIGNED);
set dig3 = CAST(RIGHT(CAST(floor(num1 / 100) as CHAR(8)), 1) as SIGNED);
set dig2 = CAST(RIGHT(CAST(floor(num1 / 10) as CHAR(8)), 1) as SIGNED);
set dig1 = CAST(RIGHT(floor(num1), 1) as SIGNED);
IF num1 != '' then
if dig6 > 0 then
case
when dig6=1 then set ans=concat(ans, 'One Hundred');
when dig6=2 then set ans=concat(ans, 'Two Hundred');
when dig6=3 then set ans=concat(ans, 'Three Hundred');
when dig6=4 then set ans=concat(ans, 'Four Hundred');
when dig6=5 then set ans=concat(ans, 'Five Hundred');
when dig6=6 then set ans=concat(ans, 'Six Hundred');
when dig6=7 then set ans=concat(ans, 'Seven Hundred');
when dig6=8 then set ans=concat(ans, 'Eight Hundred');
when dig6=9 then set ans=concat(ans, 'Nine Hundred');
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 || dig6 != 0) then
set ans=concat(ans, ' Thousand ');
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=0 then set ans=concat(ans, ' Zero');
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;
end if;
IF num2 > 0 then
set dig2 = CAST(RIGHT(CAST(floor(num2 / 10) as CHAR(8)), 1) as SIGNED);
set dig1 = CAST(RIGHT(floor(num2), 1) as SIGNED);
set ans=concat(ans, ' POINT ');
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=0 then set ans=concat(ans, ' Zero');
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;
END IF;
return trim(ans);
END$$
DELIMITER ;
答案 4 :(得分:0)
印度标准中的Mysql函数
CREATE DEFINER=`root`@`%` FUNCTION `number_to_string`(n INT) RETURNS varchar(1000) CHARSET latin1
BEGIN
declare ans varchar(1000);
declare dig1, dig2, dig3,dig4,dig5,dig6,dig7,dig8,dig9 int;
set ans = '';
set dig9 = floor(n/100000000);
set dig8 = floor(n/10000000) - dig9*10;
set dig7 = floor(n/1000000) -(floor(n/10000000)*10);
set dig6 = floor(n/100000) - (floor(n/1000000)*10);
set dig5 = floor(n/10000) - (floor(n/100000)*10);
set dig4 = floor(n/1000) - (floor(n/10000)*10);
set dig3 = floor(n/100) - (floor(n/1000)*10);
set dig2 = floor(n/10) - (floor(n/100)*10);
set dig1 = n - (floor(n / 10)*10);
if dig7 = 1 then
case
when (dig7*10 + dig6) = 10 then set ans=concat(ans,'Ten Lakhs');
when (dig7*10 + dig6) = 11 then set ans=concat(ans,'Eleven Lakhs');
when (dig7*10 + dig6) = 12 then set ans=concat(ans,'Twelve Lakhs');
when (dig7*10 + dig6) = 13 then set ans=concat(ans,'Thirteen Lakhs');
when (dig7*10 + dig6) = 14 then set ans=concat(ans,'Fourteen Lakhs');
when (dig7*10 + dig6) = 15 then set ans=concat(ans,'Fifteen Lakhs');
when (dig7*10 + dig6) = 16 then set ans=concat(ans,'Sixteen Lakhs');
when (dig7*10 + dig6) = 17 then set ans=concat(ans,'Seventeen Lakhs');
when (dig7*10 + dig6) = 18 then set ans=concat(ans,'Eighteen Lakhs');
when (dig7*10 + dig6) = 19 then set ans=concat(ans,'Nineteen Lakhs');
else set ans=ans;
end case;
else
if 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;
if ans <> '' and dig6 =0 then
set ans=concat(ans, ' Thousand');
end if;
end if;
if ans <> '' and dig6 > 0 and dig7 =0 then
set ans=concat(ans, ' And ');
end if;
if dig6 > 0 then
case
when dig6=1 then set ans=concat(ans, ' One Lakhs');
when dig6=2 then set ans=concat(ans, ' Two Lakhs');
when dig6=3 then set ans=concat(ans, ' Three Lakhs');
when dig6=4 then set ans=concat(ans, ' Four Lakhs');
when dig6=5 then set ans=concat(ans, ' Five Lakhs');
when dig6=6 then set ans=concat(ans, ' Six Lakhs');
when dig6=7 then set ans=concat(ans, ' Seven Lakhs');
when dig6=8 then set ans=concat(ans, ' Eight Lakhs');
when dig6=9 then set ans=concat(ans, ' Nine Lakhs');
else set ans = ans;
end case;
end if;
end if;
if ans <> '' and dig5 > 0 then
set ans=concat(ans, '');
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;
if ans <> '' and dig4 =0 then
set ans=concat(ans, ' Thousand');
end if;
end if;
if ans <> '' and dig4 > 0 and dig5 =0 then
set ans=concat(ans, ' And ');
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;
end if;
if ans <> '' and dig3 > 0 then
set ans=concat(ans, ' And ');
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 ans <> '' and dig2 > 0 then
set ans=concat(ans, ' And ');
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 ans <> '' and dig1 > 0 and dig2 =0 then
set ans=concat(ans, ' And ');
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 trim(ans);
END
答案 5 :(得分:0)
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
$$
答案 6 :(得分:-1)
PROCEDURE TO GENERATE NUMBER TO WORDS FORMAT...........
CREATE DEFINER=`root`@`localhost` PROCEDURE `xnum2txt`(in nNum int(8))
BEGIN
declare cRetVal,cTxt,cX1,cX2 varchar(500);
set nNum=floor(nNum);
set cRetval='';
set cTxt='';
if nnum<0 then
set cRetval="-"+ xNum22Txt(nnum*-1);
elseif nNum<11 then
set cTxt="Zero One Two ThreeFour Five Six SevenEightNine Ten ";
set cRetVal= trim(substring(cTxt,(nNum * 5) + 1,5)) ;
ELSEIF nNum < 20 then
set cTxt = "Eleven Twelve Thirteen Fourteen Fifteen Sixteen SeventeenEighteen Nineteen ";
set cRetval = trim(substring(cTxt,((nNum - 11) * 9) + 1,9));
ELSEIF nNum < 30 then
set cRetval = "Twenty" + if(nNum > 20,"-" + xnum22txt(nNum-20)," ");
ELSEIF nNum < 40 then
set cRetval = "Thirty" + if(nNum > 30,"-" + xnum22txt(nNum-30)," ");
ELSEIF nNum < 50 then
set cRetval = "Forty" + if(nNum > 40,"-" + xnum22txt(nNum-40)," ");
ELSEIF nNum < 60 then
set cRetval = "Fifty" + if(nNum > 50,"-" + xnum22txt(nNum-50)," ");
ELSEIF nNum < 70 then
set cRetval = "Sixty" + if(nNum > 60,"-" + xnum22txt(nNum-60)," ");
ELSEIF nNum < 80 then
set cRetval = "Seventy" + if(nNum > 70,"-" + xnum22txt(nNum-70)," ");
ELSEIF nNum < 90 then
set cRetval = "Eighty" + if(nNum > 80,"-" + xnum22txt(nNum-80)," ");
ELSEIF nNum < 100 then
set cRetval = "Ninety" + if(nNum > 90,"-" + xnum22txt(nNum-90)," ");
ELSEIF nNum < 1000 then
set cX1 = floor(nNum/100);
set cX2 = nNum-(cX1*100);
set cRetval = xnum22txt(cX1) + "Hundred" + if(cX2 > 0," " + xnum22txt(cX2)," ");
ELSEIF nNum < 1000000 then
set cX1 = floor(nNum/1000);
set cX2 = nNum-(cX1*1000);
set cRetval = xnum22txt(cX1) + "Thousand" + if(cX2 > 0,", " + xnum22txt(cX2)," ");
ELSEIF nNum < 1000000000 then
set cX1 = floor(nNum/1000000);
set cX2 = nNum-(cX1*1000000);
set cRetval = xnum22txt(cX1) + "Million" + if(cX2 > 0,", " + xnum22txt(cX2)," ");
ELSEIF nNum < 1000000000000 then
set cX1 = floor(nNum/1000000000);
set cX2 = nNum-(cX1*1000000000);
set cRetval = xnum22txt(cX1) + "Billion" + if(cX2 > 0,", " + xnum22txt(cX2)," ");
ELSE
set cRetval = "***";
end if;
select cRetVal;
END