Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

sql - Converting Numbers to Words in MYSQL result! Using Query

I am trying to print a output as follows when I execute a query in mysql.

Name    Salary  Sal_in_Words
Mohan   45000   Rupees Forty Five Thousand Only

The column Salary has a value 45000 and in the third column the Value in second colunm is converted to words through Query.

I found some article where in Oracle we can get the result as above using the below query:

select Salary, (' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.')) Sal_in_Words from employee

OUTPUT:

Name    Salary  Sal_in_Words
Suresh  45000   Rupees Forty Five Thousand Only

In MySQL I tried LPAD,RPAD but those just adds strings to the result not converting words to string.

I found some tutorial but all those explained about "TO_CHAR (dates)".

Is there any way to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is not simple function in MySQL you need to write a function with the help of the function you can achieve this result.

Check the following.. It works for me.. 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 ;

If get the following error..

#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)

Run this query:

SET GLOBAL log_bin_trust_function_creators = 1;

After that Create a function in Mysql:

Call the function Just like the below command:

SELECT number_to_string( 666 );

You will get the output as:

number_to_string( 666 )
six hundred sixty six

Hope this will help for others!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...