Db2中的LOCATE_IN_STRING函数

时间:2019-05-14 23:41:05

标签: db2

使用if(isset($_POST['newPost'])) { DB::query('INSERT INTO heatmap(lon, lan) VALUES (:lon, :lan)', array(':lon'=>$VAL, ':lan'=>$VAL)); } 中的LOCATE_IN_STRING函数,我想从字符串下面分离字段2和字段3。

Db2

字段1:Chk编号字段

字段2:Tr日期字段

字段3:原因

我只想提取TR日期和原因

RTN1319 5.7.18 INSUFFICIENT FUNDS

1 个答案:

答案 0 :(得分:0)

对于所有Db2 for LUW版本:

with t (s) as (values 
  'RTNCK1319  5.7.19   INSUFFICIENT FUNDS'
)
select 
  xmlcast(xmlquery('fn:tokenize($s, " +")[2]' passing ltrim(t.s) as "s") as varchar(10))
, xmlcast(xmlquery('fn:replace($s, "^ *[^ ]+ +[^ ]+ +", "")' passing t.s as "s") as varchar(100))
from t;

没有regexp(前两个中间空格必须为单个):

with t (s) as (values 
  'RTNCK1319 5.7.19 INSUFFICIENT FUNDS'
)
select 
  substr(s, s1pos+1, s2pos-s1pos) s1
, substr(s, s2pos+1) s2
from 
(
select 
  ltrim(s) s 
, locate_in_string(ltrim(s), ' ', 1, 1) s1pos
, locate_in_string(ltrim(s), ' ', 1, 2) s2pos
from t
);