2012-04-27 43 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

相關問題