mysql - 在 MYSQL 结果中将数字转换为单词!使用查询

标签 mysql sql database oracle

当我在 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(日期)”。

有什么办法吗?

最佳答案

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

希望这对其他人有帮助!

关于mysql - 在 MYSQL 结果中将数字转换为单词!使用查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435879/

相关文章:

c++ - 从 MySQL 选择 LONGTEXT 列时 CRecordset 出现 "out of memory"异常

mysql - MySQL 查询的复杂问题

php - 将在线和离线聊天用户的更新列表从 php 发送到 ios

php - SQL:如何每天选择一行

sql - 将另一个表中的数据放入检查约束

ruby-on-rails - rails 测试和开发数据库处于不同的模式中

php - 将 mysql 更改为 mysqli 时出现 fatal error : Using $this when not in object context in,

带有 WHERE 子句的多个表的 mysqldump

SQL Server 子查询返回了 1 个以上的值。当尝试从另一个选择插入时子查询跟随时,这是不允许的

mysql - 无论如何,我可以使用单个 MySQL 查询在多个表中插入记录吗?