如何用单词替换字母数字字符串中的数字

Mah*_*rpe 1 sql oracle number-formatting

我有一个名为department_details 的表,其中包含一个dept_id 列,其中包含类似的值

10_prod
20_r&d
80_sales
Run Code Online (Sandbox Code Playgroud)

等我想要一个查询,它会给我输出

ten_prod
twenty_r&d
eighty_sales
Run Code Online (Sandbox Code Playgroud)

等等。

Lit*_*oot 5

这是一种选择:

SQL> with test (col) as
  2    (select '10_prod'  from dual union all
  3     select '20_r&d'   from dual union all
  4     select '80_sales' from dual
  5    )
  6  select col,
  7    regexp_substr(col, '^\d+') num,
  8    to_char(to_date(substr(col, 1, instr(col, '_') - 1), 'j'), 'jsp') wrd,
  9    --
 10    to_char(to_date(substr(col, 1, instr(col, '_') - 1), 'j'), 'jsp') ||
 11    substr(col, instr(col, '_')) result
 12  from test;

COL      NUM                              WRD        RESULT
-------- -------------------------------- ---------- --------------------
10_prod  10                               ten        ten_prod
20_r&d   20                               twenty     twenty_r&d
80_sales 80                               eighty     eighty_sales

SQL>
Run Code Online (Sandbox Code Playgroud)

它有什么作用(一步一步,以便您可以遵循它):

  • 第 1 - 5 行:样本数据
  • 第 7 行:从字符串开头提取数字的一种方法(使用正则表达式)
  • 第 8 行:另一种方式(使用substr+ instr;可能更好)。它 - 此外 - 使用“J”格式将其转换为日期,并使用 JSP 格式将其转换为字符。这是拼写数字的常用方法
  • 第 10 - 11 行:将拼写数字(第 10 行)与字符串的其余部分(第 11 行)组合起来