0

我在供应商数据库中有一些奇怪的数据,但需要能够从数据库中的一个字段中提取多个不同的参数。

所以从这个例子中我想拉出所有介于 (" % ") 之间的项目

引号之间是一个字符串,忽略它看起来像代码:

"Func_GetParameterLatestValue("IBW 患者身高 RT 评估") kHeight =Func_GetParameterLatestValue("IBW 通风口其他高度") 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= Round(((kHeight - 152.4)*.91)+45.5,0) End IF Else Return_Value = NULL End IF End IF ' Return_Value = kHeight '( "IBW 患者身高 RT 评估")"

所以返回值是:

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

我愿意接受任何建议来尝试完成这项工作。理想情况下,我希望能够在子查询中猛烈抨击结果,以确保它们存在于另一个表中。

此查询当前返回第一个实例

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
4

1 回答 1

2

您可以使用 WITH 语句递归地执行此操作。这是一个镜头。将 varchar(max) 更改为您的 FormulaDetails 列的任何数据类型。如果您需要,此查询将返回 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;
于 2012-04-28T02:35:40.323 回答