RSS

How to convert Value Pair list to Table in SQL Server?

05 May

key-value-pair-mongoDB

DECLARE @NameValuePairs VARCHAR(MAX) = 'FirstName=Ahmed;LastName=Negm;Age=26;Address=Egypt;Job=Senior Software Developer'
DECLARE @NameValuePair VARCHAR(100)
DECLARE @Name VARCHAR(50)
DECLARE @Value VARCHAR(50)
DECLARE @Property TABLE (
 [Name] VARCHAR(50),
 [Value] VARCHAR(50)
)
WHILE LEN(@NameValuePairs) > 0
BEGIN
 SET @NameValuePair = LEFT(@NameValuePairs, 
 ISNULL(NULLIF(CHARINDEX(';', @NameValuePairs) - 1, -1),
 LEN(@NameValuePairs)))
 SET @NameValuePairs = SUBSTRING(@NameValuePairs,
 ISNULL(NULLIF(CHARINDEX(';', @NameValuePairs), 0),
 LEN(@NameValuePairs)) + 1, LEN(@NameValuePairs))
SET @Name = SUBSTRING(@NameValuePair, 1, CHARINDEX('=', @NameValuePair) - 1)
SET @Value = SUBSTRING(@NameValuePair, CHARINDEX('=', @NameValuePair) + 1, LEN(@NameValuePair))
INSERT INTO @Property ( [Name], [Value] ) VALUES ( @Name, @Value )
END

SELECT * FROM @Property

2014-05-05_12-01-00

 
Leave a comment

Posted by on May 5, 2014 in Common

 

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: