Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I'm having a problem in string manipulation in SQL SERVER 2008
This is in one field only and I need to extract this into FIELD NAME and VALUE.

Field names are those in between colon and semi-colon (these are the capital letters)
Field values are those after the colon and before the next semi-colon.

Please help me


BENEACCTNAME:Juan Dela Cruz; BENEACCT:0123456; BENEBNKNAME:Dummy Bank; BENEBNKADD:123 32nd Ave, Earth; BENEBNKMET:BCD; BENEBNKCODE:1111133333; BENEBNKCC:AR; INTBNKNAME:; INTBNKADD:; INTBNKMET:; INTBNKCODE:; PAYDET1:DetailsAcc; PAYDET2:; PAYDET3:; PAYDET4:; BNKTOBNKINFO1:; BNKTOBNKINFO2:; BNKTOBNKINFO3:; BNKTOBNKINFO4:; BNKTOBNKINFO5:; BNKTOBNKINFO6:; BENEBNKADD1:; BENEBNKADD2:; BENEBNKADD3:;

this is the supposed output

BENEACCTNAME | BENEACCT | BENEBNKNAME | BENEBNKADD | BENEBNKMET
Juan Dela Cruz | 0123456 | Dummy Bank | 123 32nd Ave, Earth | BCD

and so on...
Posted
Updated 9-Dec-15 21:19pm
v3

Here is the complete source code according to your requirements.
It store the name, value pair in Dictionary.
C#
Dictionary<string, string> dict = new Dictionary<string, string>();
            String s = "BENEACCTNAME:Juan Dela Cruz; BENEACCT:0123456; BENEBNKNAME:Dummy Bank; " +
                        "BENEBNKADD:123 32nd Ave, Earth; BENEBNKMET:BCD; BENEBNKCODE:1111133333; " +
                        "BENEBNKCC:AR; INTBNKNAME:; INTBNKADD:; INTBNKMET:; INTBNKCODE:; PAYDET1:DetailsAcc; " +
                        "PAYDET2:; PAYDET3:; PAYDET4:; BNKTOBNKINFO1:; BNKTOBNKINFO2:; BNKTOBNKINFO3:; BNKTOBNKINFO4:; " +
                        "BNKTOBNKINFO5:; BNKTOBNKINFO6:; BENEBNKADD1:; BENEBNKADD2:; BENEBNKADD3:;";
            var split = s.Split(';');
            foreach (var item in split)
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                {
                    string name = item.Split(':')[0];
                    string value = item.Split(':')[1];
                    dict.Add(name, value);
                }
            }
 
Share this answer
 
Comments
bjay tiamsic 10-Dec-15 3:20am    
Hi. Thank you for your response. I forgot to put the language im using. I am doing this in SQL Server 2008
phil.o 10-Dec-15 3:45am    
You should not execute several times the same method with the same parameter(s). I'm talking about item.Split() method. You should cache the result and use it afterwards. i.e., better store the result of the Split method in a string[] variable.
Saad Saadi 10-Dec-15 6:03am    
thanks, I'll try to do the same as you said.
Here is SQL Server:

SQL
declare @Input NVARCHAR(MAX)
set @Input = 'BENEACCTNAME:Juan Dela Cruz; BENEACCT:0123456; BENEBNKNAME:Dummy Bank; BENEBNKADD:123 32nd Ave, Earth; BENEBNKMET:BCD; BENEBNKCODE:1111133333; BENEBNKCC:AR; INTBNKNAME:; INTBNKADD:; INTBNKMET:; INTBNKCODE:; PAYDET1:DetailsAcc; PAYDET2:; PAYDET3:; PAYDET4:; BNKTOBNKINFO1:; BNKTOBNKINFO2:; BNKTOBNKINFO3:; BNKTOBNKINFO4:; BNKTOBNKINFO5:; BNKTOBNKINFO6:; BENEBNKADD1:; BENEBNKADD2:; BENEBNKADD3:;'

DECLARE @Output TABLE (
	  name NVARCHAR(100),
      value NVARCHAR(100)
)

DECLARE @StartIndex INT, @EndIndex INT, @name NVARCHAR(100), @value NVARCHAR(100), @Character CHAR(1)
SET @Character = ';'
 
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
    SET @Input = @Input + @Character
END

WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
	SET @EndIndex = CHARINDEX(@Character, @Input)
	
	IF(@EndIndex > 1)
	BEGIN
		declare @index int
		set @index = CHARINDEX(':', @Input)
		SET @name = SUBSTRING(@Input, @StartIndex, @index - 1)
		SET @value = SUBSTRING(@Input, @index + 1, @EndIndex - @index - 1)
		INSERT INTO @Output(name, value) VALUES (@name, @value)
	END
	
    SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END

select * from @Output


Regards,
Saad
 
Share this answer
 
Comments
bjay tiamsic 10-Dec-15 21:23pm    
Thank you! I got the result. However, I wanted to make the first column as ColumnNames and their respective value as row

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900