Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 String Parsing in SQL

Author  Topic 

support-an
Starting Member

1 Post

Posted - 2014-07-21 : 02:27:00
I want to grab some key value pairs from the text in sql column

e.g.
some text[Key1=Val1]some text[Key2=Val2][Key3=Val3]some text

i want a function which takes Keyname as input & returns the value related with it if found.

GetValueFmKey('Key1') should return Val1 and like on.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-07-22 : 07:52:18
Something like this may work

declare @string varchar(1000), @key varchar(100)
set @string='some text[Key1=Val1]some text[Key2=Val2][Key3=Val3]some text'
set @key='key1'
select left(starting,charindex(']',starting)-1) from
(select substring(@string,charindex(@key,@string)+len(@key)+1,100) as starting
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -