我想从字符串中获取特定值

时间:2016-12-25 06:16:39

标签: sql sql-server tsql sql-server-2012

字符串1:

{cm_documentation_.chk_phone_call_physician}=1 & 
{cm_documentation_.txt_phone_call_code}="99441"  &   
({cm_documentation_.txt_units_mins}!="" & ({local.units_mins}<5 |
{local.units_mins}>10) | {cm_documentation_.txt_units_mins}="")

字符串2:

{@This}="93015" | {@This}="78454" | {@This}="78453" | {@This}="78452" |   
{@This}="78451" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459"  

我想在=符号

之后获取值
Result: 
99441
93015
78454
78453

。 。 。 。 。 。 。 所以......

2 个答案:

答案 0 :(得分:1)

不知道你在寻找什么是String1

但是,使用Parse / Split功能

Class myClass (object):
    def setup(self):
        #something here

<强>返回

Declare @String2 varchar(max) = '{@This}="93015" | {@This}="78454" | {@This}="78453" | {@This}="78452" |  
{@This}="78451" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459" '

Select RetSeq
      ,RetVal=Replace(Substring(RetVal,PatIndex('%[0-9]%',RetVal),25),'"','') 
 From [dbo].[udf-Str-Parse](@String2,'|')

UDF(如果需要)

RetSeq  RetVal
1       93015
2       78454
3       78453
4       78452
5       78451
6       78480
7       78478
8       78465
9       78499
10      78492
11      78491
12      78459

答案 1 :(得分:0)

你可以通过几种方式来解决这个问题。为了得到我认为OP正在寻找解决方案的东西,你可以对John Cappelletti的解决方案稍作调整,看起来像这样:

DECLARE 
@string1 varchar(1000) = 
'{cm_documentation_.chk_phone_call_physician}=1 & 
{cm_documentation_.txt_phone_call_code}="99441"  &   
({cm_documentation_.txt_units_mins}!="" & ({local.units_mins}<5 |
{local.units_mins}>10) | {cm_documentation_.txt_units_mins}="")',

@string2 varchar(1000) = 
'{@This}="93015" | {@This}="78454" | {@This}="78453" | {@This}="78452" |   
{@This}="78451" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459"';

Select RetVal
From [dbo].[udf-Str-Parse](@string1 + @string2,'"')
where RetVal NOT LIKE '%[^0-9]%';

请注意,我知道SQL 2012+的最快字符串“拆分器”here

另一种选择是使用PatternSplitCM,你可以获得here。这样做:

DECLARE 
@string1 varchar(1000) = 
'{cm_documentation_.chk_phone_call_physician}=1 & 
{cm_documentation_.txt_phone_call_code}="99441"  &   
({cm_documentation_.txt_units_mins}!="" & ({local.units_mins}<5 |
{local.units_mins}>10) | {cm_documentation_.txt_units_mins}="")',

@string2 varchar(1000) = 
'{@This}="93015" | {@This}="78454" | {@This}="78453" | {@This}="78452" |   
{@This}="78451" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459"';

SELECT item = REPLACE(item,'"','')
FROM dbo.PatternSplitCM(@string1 + @string2,'[0-9"]')
WHERE matched = 1
AND item LIKE '"[0-9]%"';

现在,如果您一直在寻找这种模式([0-9] =数字):=“[0-9] [0-9] [0-9] [0-9] [0- 9]“最快的方法就是像这样使用NGrams8K

DECLARE 
@string1 varchar(1000) = 
'{cm_documentation_.chk_phone_call_physician}=1 & 
{cm_documentation_.txt_phone_call_code}="99441"  &   
({cm_documentation_.txt_units_mins}!="" & ({local.units_mins}<5 |
{local.units_mins}>10) | {cm_documentation_.txt_units_mins}="")',

@string2 varchar(1000) = 
'{@This}="93015" | {@This}="78454" | {@This}="78453" | {@This}="78452" |   
{@This}="78451" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459"';

SELECT token = SUBSTRING(token,3,5)
FROM dbo.NGrams8K(@string1 + @string2,8)
WHERE token LIKE '="[0-9][0-9][0-9][0-9][0-9]"';

或者,假设您知道您要解析的数字是3到5个字符长 - 您可以使用NGrams8K这样做:

DECLARE 
@string1 varchar(1000) = 
'{cm_documentation_.chk_phone_call_physician}=1 & 
{cm_documentation_.txt_phone_call_code}="99441"  &   
({cm_documentation_.txt_units_mins}!="" & ({local.units_mins}<5 |
{local.units_mins}>10) | {cm_documentation_.txt_units_mins}="")',

@string2 varchar(1000) = 
'{@This}="9301" | {@This}="78454" | {@This}="78453" | {@This}="78452" |   
{@This}="784514" | {@This}="78480" | {@This}="78478" | {@This}="78465" |  
{@This}="78499" | {@This}="78492" | {@This}="78491" | {@This}="78459"';

SELECT token = SUBSTRING(token, 3, stringlen)
FROM (VALUES (4),(5),(6)) s(stringlen)
CROSS APPLY dbo.NGrams8K(@string1+@string2, stringlen+3)
WHERE token LIKE '="'+REPLICATE('[0-9]',stringlen)+'"';