小编典典

如何在SQL Server中提取子字符串

sql

我有一个类似的领域 okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos

我想name = value从数据中提取。

如何使用SQL Server 2008高效地做到这一点?谢谢你


阅读 216

收藏
2021-04-07

共1个答案

小编典典

试试这个

;
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)
2021-04-07