我有以下数据: -
1)您指定的运营商是{carrier_name}。他们会尽快与您联系以安排接机,但您可以随时致电{carrier_phone}并参考{reference_id}。它们现在是开放的,直到{close_time}。
2){vehicle_owner}表示他们的{body_style}已被选中。您是否会提供有关作业的更新或标记完成?
我想找到括号中的所有值 - > {___}
。
在括号内只能找到特定的消息。它们之间可能有任何价值。
如何使用查询找到它?
答案 0 :(得分:2)
如果字符串始终遵循for i in 1..4 {
if stairs >= i {
count += options(stairs - i);
}
}
的重复模式,则解决此问题的一种方法使用Jeff Moden的CSV分割器函数,用第一个分隔符替换第二个分隔符,并使用{{3获取第二个分隔符}}:
'..{..})'
测试设置:modulo (%
)
返回:
select
Id
, col = x.item
from t
cross apply (
select Item = ltrim(rtrim(i.Item))
from [dbo].[delimitedsplit8K](replace(t.col,'}','{'),'{') as i
where ItemNumber%2=0
) x
拆分字符串参考:
测试中使用的函数:
+----+---------------+
| Id | col |
+----+---------------+
| 1 | carrier_name |
| 1 | carrier_phone |
| 1 | reference_id |
| 1 | close_time |
| 2 | vehicle_owner |
| 2 | body_style |
+----+---------------+
递归cte版本(无需额外功能)
create function [dbo].[delimitedsplit8K] (
@pstring varchar(8000)
, @pdelimiter char(1)
)
returns table with schemabinding as
return
with e1(N) as (
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all select 1
)
, e2(N) as (select 1 from e1 a, e1 b)
, e4(N) as (select 1 from e2 a, e2 b)
, ctetally(N) as (
select top (isnull(datalength(@pstring),0))
row_number() over (order by (select null)) from e4
)
, ctestart(N1) as (
select 1 union all
select t.N+1 from ctetally t where substring(@pstring,t.N,1) = @pdelimiter
)
, ctelen(N1,L1) as (
select s.N1,
isnull(nullif(charindex(@pdelimiter,@pstring,s.N1),0)-s.N1,8000)
from ctestart s
)
select itemnumber = row_number() over(order by l.N1)
, item = substring(@pstring, l.N1, l.L1)
from ctelen l
;
返回:
;with cte as (
select
id
, val = left(stuff(col, 1, charindex('{', col),'')
, charindex('}', col) - charindex('{', col) - 1
)
, rest = stuff(col, 1, charindex('}', col) + 1,'')
from t
where col like '%{%}%'
union all
select
id
, val = left(stuff(rest, 1, charindex('{', rest),'')
, charindex('}', rest) - charindex('{', rest) - 1
)
, rest = stuff(rest, 1, charindex('}', rest) + 1,'')
from cte
where rest like '%{%}%'
)
select id, val
from cte
order by id, val;
答案 1 :(得分:-1)
这可以通过 SUBSTRING 和 CHARINDEX
来实现
SUBSTRING(@Text, CHARINDEX('%{%',@Text), CHARINDEX('%}%',@Text))
CHARINDEX给出搜索字符串第一次出现的值。在SUBSTRING中提供这些值,您可以获得所需的输出。