1

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

I'd like to extract the name = value from the data.

How can I do that using SQL Server 2008 in efficient way? thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Himberjack
  • 5,682
  • 18
  • 71
  • 115

4 Answers4

5

Try this

;
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)
Raj More
  • 47,048
  • 33
  • 131
  • 198
  • I need to SELECT a field from the DB first.. I cant use hard coded value – Himberjack Jan 25 '11 at 13:00
  • @oshafran See my first select under the WITH - replace that with your SELECT statement such as `SELECT Youfield as FullString From YourTable` – Raj More Jan 25 '11 at 13:04
  • Invalid length parameter passed to the LEFT or SUBSTRING function. – Himberjack Jan 25 '11 at 13:11
  • @oshafran that was happening to you because you did not have a `&name=` terminated by `&`. I modified the query to do the proper thing. – Raj More Jan 25 '11 at 13:16
  • @Raj More: If the string ends with `name=...` (i.e. no terminating `&`), the substring is not extracted with your query. Although the OP may have no objection to such a case. – Andriy M Jan 28 '11 at 09:38
  • @Andriy M, I modified the SQL to append an ampersand to account for this – Raj More Jan 28 '11 at 15:25
  • @Raj More: It works now all right, +1. Only one more thing that I had to fix when I tested it: after your last change the script became broken, because `FullString + '&'` was not aliased. – Andriy M Jan 28 '11 at 16:45
1

This will work I think:

Declare @String varchar(100)

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

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

This produces:

'name=fokdofkd&okfaos'

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
1
SELECT 'name=' + SUBSTRING(field, CHARINDEX('name=', field), CHARINDEX('&', SUBSTRING(field, CHARINDEX('name=' ,field))))
Matten
  • 17,365
  • 2
  • 42
  • 64
1

Something along the lines of...

select substring(ColumnName, charindex('name=',ColumnName,1),len(ColumnName))
grapefruitmoon
  • 2,988
  • 1
  • 22
  • 26