简体   繁体   English

以字符串形式返回子字符串的所有结果

[英]Return all results of substring with in string

I have some odd data in a vendor database but need to be able to extract multiple different parameters from one field in the db. 我在供应商数据库中有一些奇怪的数据,但需要能够从数据库中的一个字段中提取多个不同的参数。

So from this example i would like to pull out all items that fall between (" % ") 所以从这个例子我想拉出所有介于两者之间的项目(“%”)

Between quotes is a string, disregard that it looks like code: 引号之间是一个字符串,不管它看起来像代码:

"Func_GetParameterLatestValue("IBW Patient Height RT Assess") kHeight =Func_GetParameterLatestValue("Height For IBW Vent Misc") If (kSex) = "" Then “Func_GetParameterLatestValue(”IBW Patient Height RT Assess“)kHeight = Func_GetParameterLatestValue(”IBW Vent Misc的高度“)If(kSex)=”“Then
Return_Value =NULL Else If kHeight > 0 Then If kSex=1 Then Return_Value= Round(((kHeight - 152.4)*.91)+50,0) Else Return_Value = NULL否则如果kHeight> 0那么如果kSex = 1那么Return_Value = Round(((kHeight-152.4)* .91)+50,0)Else
Return_Value= Round(((kHeight - 152.4)*.91)+45.5,0) End IF Else Return_Value = NULL End IF End IF ' Return_Value = kHeight '("IBW Patient Height RT Assess")" Return_Value = Round(((kHeight - 152.4)* .91)+45.5,0)结束IF Else Return_Value = NULL结束IF结束IF'Return_Value = kHeight'(“IBW患者身高RT评估”)“

so the return values would be: 所以返回值是:

IBW Patient Height RT Assess,
Height For IBW Vent Misc,
IBW Patient Height RT Assess

Im open to any suggestions to try and make this work. 我愿意接受任何建议来尝试这项工作。 Ideally i would like to be able to slam the results in a subquery as well to make sure that they exist on another table. 理想情况下,我希望能够在子查询中抨击结果,以确保它们存在于另一个表中。

This query currently returns the first instance 此查询当前返回第一个实例

select vbs.Name, 
        SUBSTRING(sd.FormulaDetails, 
                  CHARINDEX('("', sd.FormulaDetails)+2,(CHARINDEX('")',sd.FormulaDetails) - CHARINDEX('("', sd.FormulaDetails))-2)
from StatementDefinitions sd, MvVBScript vbs
where sd.ScriptID = vbs.ID

You can do this recursively with a WITH statement. 您可以使用WITH语句递归执行此操作。 Here's a shot at it. 这是一个镜头。 Change varchar(max) to whatever the data type of your FormulaDetails column is. 将varchar(max)更改为FormulaDetails列的数据类型。 In case you want it, this query returns the ScriptID and numbers the position of the chunk it finds (so 'Height For IBW Vent Misc' would be occurrence 2) 如果你需要它,这个查询返回ScriptID并对它找到的块的位置进行编号(因此'IBW Vent Misc的高度'将出现2)

with Chunks(id,occurrence,position,token,remainder) as (
  select
    ScriptID,
    cast(0 as int),
    charindex(@token,FormulaDetails),
    cast('' as varchar(max)),
    substring(c,charindex(@token,FormulaDetails)+1,len(FormulaDetails))
  from StatementDefinitions
  where FormulaDetails like '%'+@token+'%'
  union all
  select
    id,
    occurrence+1,
    charindex(@token,remainder)+position,
    cast(substring(remainder,1,charindex(@token,remainder)-1) as varchar(max)),
    substring(remainder,charindex(@token,remainder)+1,len(remainder))
  from Chunks
  where remainder like '%'+@token+'%'
)
  select id, occurrence, token from Chunks
  where occurrence > 0
  order by id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM