2011-01-25 81 views
1

子我有一個領域的東西,如okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos如何提取在SQL Server

我想從數據中提取的name = value

如何以有效的方式使用SQL Server 2008? 謝謝

回答

5

試試這個

; 
With MyTable as 
(
    SELECT 'okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos' FullString 
    UNION 
    SELECT 'fkgbokfgbo&name=fokdofkd&okfaos' FullString 
    UNION 
    SELECT 'okbogkbogkbokgbok' FullString 
), 
PatternIndex as 
(
    Select 
     FullString + '&' FullString, 
     CharIndex ('&name=', FullString) + 1 LocationOfEqualSign, 
     CharIndex ('&', FullString, CharIndex ('&name=', FullString)+1) as NextBreak 
    from MyTable 
), 
SplitNameValue AS 
(
    Select 
     FullString, 
     CASE 
      WHEN NextBreak <> 0 THEN 
      SubString (FullString, LocationOfEqualSign, NextBreak-LocationOfEqualSign) 
      ELSE '' END 
     as NameValuePair 
    From PatternIndex 
) 
SELECT * 
FROM SplitNameValue 

返回

FullString            NameValuePair 
--------------------------------------------------------- --------------------------------------------------------- 
fkgbokfgbo&name=fokdofkd&okfaos       name=fokdofkd 
okbogkbogkbokgbok           
okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos name=fokdofkd 

(3 row(s) affected) 
1

這工作,我想:

Declare @String varchar(100) 

Set @String = 'okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos' 

Select SubString(@String,CharIndex('name=',@String),len(@String)) 

這將產生:

'名稱= fokdofkd & okfaos'

+1

長度9999是不正確的,因爲可以按照更&name = value的 – Matten 2011-01-25 12:56:40

+0

@Matten - 你怎麼知道的?這在問題中沒有說明。 – 2011-01-25 12:57:32

1
SELECT 'name=' + SUBSTRING(field, CHARINDEX('name=', field), CHARINDEX('&', SUBSTRING(field, CHARINDEX('name=' ,field)))) 
1

東西線沿線的...

select substring(ColumnName, charindex('name=',ColumnName,1),len(ColumnName))