How to extract a substring in SQL Server

I have a field with something like okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos

I want to extract name = valuefrom the data.

How can I use SQL Server 2008 effectively? thank

+1
source share
4 answers

try it

;
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

Returns

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

(3 row(s) affected)
+5
source

This will work, I think:

Declare @String varchar(100)

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

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

This gives:

'name = fokdofkd & okfaos'

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

Something along the lines ...

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

Source: https://habr.com/ru/post/1788806/


All Articles