这是一个例子:
[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14
我只需要将 118.7->59.35 提取为 Tipton,并在另一列中提取 428.28->214.14 作为 U_Haulage。
字符串的长度和我的模式词的位置一样是可变的。
我正在尝试使用 Patindex,但找不到方法。
In MySQL there's SUBSTRING_INDEX, which extracts a substring based on a delimiter:
select
substring_index(substring_index(x, '[U_TipTon]=', -1), ';', 1) as TipTon
,substring_index(substring_index(x, '[U_Haulge]=', -1), ';', 1) as Haulge
from
(
select '[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14' as x
) as dt
Edit: In MS SQL Server it's more complicated:
select
substring(xHaulge, 1, charindex(';', xHaulge + ';')-1) as Haulge,
substring(xTipTon, 1, charindex(';', xTipTon + ';')-1) as TipTon
from
(
select
case when charindex('[U_Haulge]=', x) > 0
then substring(x, charindex('[U_Haulge]=', x) + len('[U_Haulge]='), 8000)
else ''
end as xHaulge,
case when charindex('[U_TipTon]=', x) > 0
then substring(x, charindex('[U_TipTon]=', x) + len('[U_TipTon]='), 8000)
else ''
end as xTipTon
from
(
select '[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14' as x
) as dt
) as dt
解决方案是:
当 charindex('Haulge',t.xField) > 0 时的情况
substring( t.xField , charindex('Haulge',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('Haulge',t.xField) + 8,LEN(t.xField) xField))) = 0 然后 LEN(t.xField) else charindex(';',substring( t.xField , charindex('Haulge',t.xField) + 8,LEN(t.xField)))-1 结束) else '-' end [Haul Price] , case when charindex('Tipton',t.xField) > 0 then
substring( t.xField , charindex('TipTon',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('Tipton',t.xField) + 8,LEN(t. xField))) = 0 然后 LEN(t.xField) else charindex(';',substring( t.xField , charindex('Tipton',t.xField) + 8,LEN(t.xField)))-1 结束) else '-' end [Tip Price] , case when charindex('AItmPr',t.xField) > 0 then
substring( t.xField , charindex('AItmPr',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('AItmPr',t.xField) + 8,LEN(t.xField) xField))) = 0 然后 LEN(t.xField) else charindex(';',substring( t.xField , charindex('AItmPr',t.xField) + 8,LEN(t.xField)))-1 结束) else '-' end [额外价格]