sql - 在字符串中搜索子字符串并除以
问题描述
我有一个包含多个句子的 varchar2(5000 char) 列的表。在这样的句子中的某处显示了一个数字,然后是另一个数字。我想选择这些数字并将它们分开。不确定如何在 sqlplus oracle 11g 中执行此操作。
我想我应该使用像SUBSTR, REGEXP_SUBSTR, INSTR
. 不确定哪个最适合这份工作。
列是这样的:
blah bahwl balwo hxkswl blahxhsh alshbhe NUM 40003.26 in 4 pieces. etc
bwh bahwl bafado hxkswl alshbhe NUM 6006.16 in 9 pieces. etc
badh baadfl balwo hxkswl blahxhsh alshbhe NUM 200 in 30 pieces. etc
bfda bdafl hxkswl NUM 33 in 4 pieces. etc
blcfh bfdwl bfdlwo alshbhe NUM 54545.01 in 700 pieces. etc
所以我想直接在NUM之后搜索并选择数字,然后选择“in”和“pieces”之间的数字,然后将这些数字相除。
select .... 40003.26 / 4
select .... 6006,16 / 9
select .... 200 / 30
等等
任何帮助表示感谢
解决方案
您可以使用regexp_substr
,regexp_replace
与l
/的贡献rtrim
如下
with t1 as
(select 'blah bahwl balwo hxkswl blahxhsh alshbhe NUM 40003.26 in 4 pieces. etc
bwh bahwl bafado hxkswl alshbhe NUM 6006.16 in 9 pieces. etc
badh baadfl balwo hxkswl blahxhsh alshbhe NUM 200 in 30 pieces. etc
bfda bdafl hxkswl NUM 33 in 4 pieces. etc
blcfh bfdwl bfdlwo alshbhe NUM 54545.01 in 700 pieces. etc' as str
from dual),
t2 as
(select regexp_substr(str, '(.*)pieces.*', 1, level) as str
from t1
cross join dual
connect by level <= regexp_count(str, 'pieces')),
t3 as
(select str,
regexp_replace(str, '[^0-9]in[^0-9]', '/') str1,
regexp_substr(str, '[^0-9]*') str2,
regexp_substr(str, '[^0-9]*$') str3
from t2)
select rtrim(ltrim(str1, str2), str3) as "Result String" from t3
Result String
-------------
40003.26/4
6006.16/9
200/30
33/4
54545.01/700
推荐阅读
- apache-nifi - 如何为自定义处理器生成使用文档
- excel - 我正在尝试使用 xlrd 检测 excel (.xlsx) 文件中单元格背景的颜色,但我似乎无法成功
- git - 如何在 gitignore 中选择多个相同格式的文件,但特定名称除外
- ruby-on-rails - Rails ajax 调用路由到错误的控制器操作
- html - 2 个 div 相互对齐并在视口中居中停止在较小的视口中对齐
- typescript - 是否建议发布 TypeScript 节点模块的源文件?
- python - TF2.0 Data API 从每个类标签中获取 n_i 个样本
- ubuntu - 有没有办法恢复崩溃的 Choregraphe 项目?
- function - 根据周开始和周结束日期生成周历
- python - 在函数/循环问题中更新访问令牌(全局变量)