在印度编号系统中将数字转换为单词

时间:2018-05-29 07:37:42

标签: sql oracle oracle11g

我正在使用此代码将数字转换为单词

select to_char(to_date(:number,'j'),'jsp') from dual;

当我输入值100000时,输出为One Hundred Thousand,但我想输出show:

One Lac

如何处理?

1 个答案:

答案 0 :(得分:3)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE numbers ( value ) AS
SELECT               0 FROM DUAL UNION ALL
SELECT          123456 FROM DUAL UNION ALL
SELECT        12345678 FROM DUAL UNION ALL
SELECT   1234567890123 FROM DUAL UNION ALL
SELECT 123456789012345 FROM DUAL UNION ALL
SELECT 900000000000000 FROM DUAL UNION ALL
SELECT 700000001200000 FROM DUAL UNION ALL
SELECT 543000000000000 FROM DUAL;

查询1

WITH rsqfc ( original, value, suffix, result, lvl, mxlvl ) AS (
  SELECT value,
         TRUNC( value / 10000000 ),
         CAST( 'CRORE ' AS VARCHAR2(4000) ),
         CAST(
           RTRIM(
             CASE
             WHEN MOD( TRUNC( value / 100000 ), 100 ) > 0
             THEN TO_CHAR(
                    TO_DATE(
                      MOD( TRUNC( value / 100000 ), 100 ),
                      'J'
                    ),
                    'JSP'
                  ) || ' LAKH '
             END
             ||
             CASE
             WHEN value = 0
             THEN 'ZERO'
             WHEN MOD( value, 100000 ) > 0
             THEN TO_CHAR(
                    TO_DATE(
                      MOD( value, 100000 ),
                      'J'
                    ),
                    'JSP'
                  ) || ' '
             END
           )
           AS VARCHAR2(4000)
         ),
         1,
         CEIL( LENGTH( ABS( value ) ) / 7 )
  FROM   numbers
UNION ALL
  SELECT original,
         TRUNC( value / 10000000 ),
         'CRORE ' || suffix,
         RTRIM(
           CASE
           WHEN MOD( TRUNC( value / 100000 ), 100 ) > 0
           THEN TO_CHAR(
                  TO_DATE(
                    MOD( TRUNC( value / 100000 ), 100 ),
                    'J'
                  ),
                  'JSP'
                ) || ' LAKH ' || suffix
           END
           ||
           CASE
           WHEN MOD( value, 100000 ) > 0
           THEN TO_CHAR(
                  TO_DATE(
                    MOD( value, 100000 ),
                    'J'
                  ),
                  'JSP'
                ) || ' ' || suffix
           END
           || result
         ),
         lvl + 1,
         mxlvl
  FROM   rsqfc
  WHERE  lvl < mxlvl
)
SELECT original, result
FROM   rsqfc
WHERE  lvl = mxlvl

<强> Results

|        ORIGINAL |                                                                                                                                           RESULT |
|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
|               0 |                                                                                                                                             ZERO |
|          123456 |                                                                                            ONE LAKH TWENTY-THREE THOUSAND FOUR HUNDRED FIFTY-SIX |
|        12345678 |                                                                        ONE CRORE TWENTY-THREE LAKH FORTY-FIVE THOUSAND SIX HUNDRED SEVENTY-EIGHT |
|   1234567890123 |                    ONE LAKH CRORE TWENTY-THREE THOUSAND FOUR HUNDRED FIFTY-SIX CRORE SEVENTY-EIGHT LAKH NINETY THOUSAND ONE HUNDRED TWENTY-THREE |
| 123456789012345 | ONE CRORE CRORE TWENTY-THREE LAKH CRORE FORTY-FIVE THOUSAND SIX HUNDRED SEVENTY-EIGHT CRORE NINETY LAKH TWELVE THOUSAND THREE HUNDRED FORTY-FIVE |
| 900000000000000 |                                                                                                                                 NINE CRORE CRORE |
| 700000001200000 |                                                                                                                    SEVEN CRORE CRORE TWELVE LAKH |
| 543000000000000 |                                                                                                          FIVE CRORE CRORE FORTY-THREE LAKH CRORE |