2012-04-27 45 views
0

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

所以从这个例子,我想退出落在(“%”)

引号之间是一个字符串,无视它看起来像代码之间的所有项目:

‘Func_GetParameterLatestValue(’IBW (kSex)=“”然后
Return_Value = NULL否则如果kHeight> 0那么如果kSex = 1那么Return_Value = Round(((kHeight - 152.4)* .91)+50,0)其他
Return_Value = Round(((kHeight-152.4)* .91)+45.5,0)End IF Else Return_Value = NULL End I六最终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 
+0

你需要澄清这个问题。你问的是如何为某些查询对列结果进行子串处理?上面的代码甚至不像T-SQL。 – Tejs 2012-04-27 17:43:20

+0

对不起,它不代表看起来像代码,它只是一个实际的字符串文本.... – cjparker 2012-04-27 20:52:14

+0

我更像是一个.NET人,而不是SQL人......所以我倾向于CLR-UDF – 2012-04-27 21:00:57

回答

2

您可以使用WITH语句递归地做到这一点。这是一个镜头。将varchar(max)更改为您的FormulaDetails列的数据类型。如果你想要的话,这个查询返回ScriptID和数字找到块的位置(这样的身高对于IBW通风杂项'将发生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 '%'[email protected]+'%' 
    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 '%'[email protected]+'%' 
) 
    select id, occurrence, token from Chunks 
    where occurrence > 0 
    order by id; 
+0

嘿史蒂夫,谢谢你。第一个子字符串中的'C'是什么? – cjparker 2012-04-30 14:19:54

+0

糟糕。 c应该是FormulaDetails,我很确定。 – 2012-04-30 15:50:35

+0

多数民众赞成多少我认为,但即时获得零结果返回:( – cjparker 2012-04-30 16:41:54

相关问题