暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 想用文字检索数字

askTom 2017-09-15
324

问题描述

我想输出为:

数字字符串
101一零一
102一零二
851八五一
9856九八五六
356三五六
748七四八
254二五四


如何以这种方式获取string。

专家解答

您可能会看到一些使用JSP格式拼写数字的示例,例如

SQL> select to_char(to_date('7','J'),'JSP') 来自dual;

TO_CH
-----
SEVEN


但这在这里行不通,因为不允许零

SQL> select to_char(to_date('0','J'),'JSP') 来自dual;
select to_char(to_date('0','J'),'JSP') 来自dual
                       *
ERROR at line 1:
ORA-01854: julian date must be between 1 and 5373484


因此,我们将只使用一个简单的CASE语句,并 “按级别连接” 来循环遍历每个字符

SQL> select
  2    case x
  3      when '0' then 'zero'
  4      when '1' then 'one'
  5      when '2' then 'two'
  6      when '3' then 'three'
  7      when '4' then 'four'
  8      when '5' then 'five'
  9      when '6' then 'six'
 10      when '7' then 'seven'
 11      when '8' then 'eight'
 12      when '9' then 'nine'
 13    end
 14  来自( select '3' x 来自dual ) ;

CASEX
-----
three

SQL>
SQL>      select
  2    case x
  3      when '0' then 'zero'
  4      when '1' then 'one'
  5      when '2' then 'two'
  6      when '3' then 'three'
  7      when '4' then 'four'
  8      when '5' then 'five'
  9      when '6' then 'six'
 10      when '7' then 'seven'
 11      when '8' then 'eight'
 12      when '9' then 'nine'
 13    end
 14  来自(
 15    select substr('123',rownum,1) x
 16    来自dual
 17    connect by level <= 3
 18    ) ;

CASEX
-----
one
two
three


因此,现在我们只需要将其合并到我们的值表中。这是我们如何获得表的每一行中的每个元素

SQL> create or replace type string_list is table of varchar2(20);
  2  /

Type created.

SQL>
SQL> create table t ( x int );

Table created.

SQL>
SQL> insert into t values (101);

1 row created.

SQL> insert into t values (456);

1 row created.

SQL> insert into t values (789);

1 row created.

SQL>
SQL> select *
  2  来自t,
  3       table(cast(multiset(
  4          select substr(to_char(t.x),rownum,1)
  5          来自dual
  6  connect by level <= length(to_char(t.x))) as string_list)
  7  );

         X COLUMN_VAL
---------- ----------
       101 1
       101 0
       101 1
       456 4
       456 5
       456 6
       789 7
       789 8
       789 9

9 rows selected.


所以现在我们只合并我们的案例陈述,我们就快到了

SQL> select
  2    x,
  3    case digit
  4      when '0' then 'zero'
  5      when '1' then 'one'
  6      when '2' then 'two'
  7      when '3' then 'three'
  8      when '4' then 'four'
  9      when '5' then 'five'
 10      when '6' then 'six'
 11      when '7' then 'seven'
 12      when '8' then 'eight'
 13      when '9' then 'nine'
 14    end str
 15  来自(
 16    select x, column_value digit
 17    来自t,
 18         table(cast(multiset(
 19            select substr(to_char(t.x),rownum,1)
 20            来自dual
 21    connect by level <= length(to_char(t.x))) as string_list)
 22    )
 23  )
 24
SQL>
SQL>
SQL> /

         X STR
---------- -----
       101 one
       101 zero
       101 one
       456 four
       456 five
       456 six
       789 seven
       789 eight
       789 nine

9 rows selected.



现在我们需要聚合这些字符串-在11g以上,您可以使用LISTAGG,但是由于您使用的是10g,因此需要自己滚动。有关详细信息,请参见此问题中的示例。

https://asktom.oracle.com/pls/apex/asktom.search?tag=stragg#16551777586484

那就只是

选择x、stragg(str)
来自
(... 以上)
按x分组

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论