【问题标题】:How can I extract only plain text from the column values in SQL?如何从 SQL 中的列值中仅提取纯文本?
【发布时间】:2021-08-21 13:09:51
【问题描述】:

我有一个包含字母数字值的列(现有列),我必须从中仅提取纯文本,如下例所示:

Existing Col Required Col
G14 - Chocolate money 543 Chocolate money
Parking amount Q89 Parking amount
19 - Chips fee - 20 Chips fee
698 Ticket fee Ticket fee

我尝试了 SUBSTRING 函数,但无法将其修改为只接收纯文本并忽略其他所有内容。

SUBSTRING ( Existing Col,5 ,10 )

【问题讨论】:

  • 可能是 regexp_replace 的东西

标签: mysql sql substring calculated-columns alphanumeric


【解决方案1】:

假设你总是至少有两个词,你可以使用:

select regexp_substr(val, '([A-Za-z]+[A-Za-z ]+[A-Za-z]+)( |$)')
from (select 'G14 - Chocolate money 543' as val union all
      select 'Parking amount Q89' union all
      select '19 - Chips fee - 20' union all
      select '698 Ticket fee'
     ) x

Here 是一个 dbfiddle。

【讨论】:

  • 感谢您的回复,它工作正常,如 dbfiddle 所示,但是如何传递列名而不是传递单个值?
  • @PreethamCS 。 . .您只需像示例中一样传递列名(其中列名为val)。
  • 对不起,当我说列名时我不清楚,我正在寻找一种方法来将列“Existing Col”传递给查询,而不是单独选择值的语句,因为那里列中有超过 100 条记录。
  • @PreethamCS。 . . regexp_substr(existing_col, '([A-Za-z]+[A-Za-z ]+[A-Za-z]+)( |$)').
猜你喜欢
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 2014-01-24
  • 2011-08-16
相关资源
最近更新 更多