I've few strings in Table1 which i want to update based on values available in Table2.
Sample code
http://rextester.com/HQFOQ18215
IF OBJECT_ID('Test1','U') iS NOT NULL
DROP TABLE Test1;
IF OBJECT_ID('Test2','U') iS NOT NULL
DROP TABLE Test2;
Create table Test1
(
Id INT
,Lid INT
,MyString VARCHAR(MAX)
,Did INT
,Secid INT
);
INSERT INTO Test1 values (1,100,'you,shall,not,pass,gandlaf,the,grey', 401, 501);
INSERT INTO Test1 values (2,100,'ok,fine,bye', 401, 501);
INSERT INTO Test1 values (3,100,'test,dev,uat,prod', 403, 501);
INSERT INTO Test1 values (4,100,'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16', 404, 501);
Create table Test2
(
Id INT IDENTITY(1,1)
,SecId INT
,CommaPosition INT
,Value VARCHAR(50)
,Did INT
,RowId INT
);
INSERT INTO Test2 Values (501, 4, '[Quantity]', 401, 1);
INSERT INTO Test2 Values (501, 14, '[Price]', 401, 1);
INSERT INTO Test2 Values (501, 4, '[Quantity]', 401, 2);
INSERT INTO Test2 Values (501, 14, '[Price]', 401, 2);
INSERT INTO Test2 Values (501, 14, '[Quantity|Price]', 403, 3);
INSERT INTO Test2 Values (501, 4, '[Interest]', 404, 4);
INSERT INTO Test2 Values (501, 14, '[Expired]', 404, 4);
SELECT * FROM Test1;
SELECT * FROM Test2;
Expected Output
/*
Expected OUTPUT
Id Lid MyString Did Secid
1 100 you,shall,not,[quantity],gandlaf,the,grey,,,,,,,[Price], 401 501
2 100 ok,fine,bye,[quantity],,,,,,,,,,[Price], 402 501
3 100 test,dev,uat,prod,,,,,,,,,,[Quantity|Price], 403 501
4 100 1,2,3,[Quantity],5,6,7,8,9,10,11,12,13,[Price],15,16 404 501
*/
First string
you,shall,not,pass,gandlaf,the,greywhere"pass"is before 4th comma position which is replaced by[quantity]from table2 but this doesn't have 14th comma so comma is replicated till it reaches the 14th comma and before 14th comma[Price]has been replaced. Final output is"you,shall,not,[quantity],gandlaf,the,grey,,,,,,,[Price],"Second string
ok,fine,byedoesn't have 4th comma either 14th comma, so 4th comma is added and before that[quantity]is substituted and then comma added till 14th position basd on Comma position column in Table2 and final string becomesok,fine,bye,[quantity],,,,,,,,,,[Price],Third-string
test,dev,uat,prodonly 14th comma position is available in Table2, so comma is replicated till 14th commas and before 14th comma[Quantity|Price]string is added final string becomestest,dev,uat,prod,,,,,,,,,,[Quantity|Price],In 4th string
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16if you see 4th and 14th become commas are available so string before 14th comma is replaced by[Interest]and string become 14th comma is replaced by[Expired]keeping the remaining string untouched.
I want to achieve above output, I tried below update statement is not giving me desired results.
UPDATE T1
SET T1.MyString = T1.MyString + REPLICATE(',',T2.CommaPosition - (len(T1.MyString) - LEN(REPLACE(T1.MyString,',',''))))
FROM Test1 as T1 INNER JOIN Test2 as T2 ON T1.Secid = T2.SecId AND T1.Did = T2.Did AND T1.Id = T2.RowId;
SELECT DISTINCT T1.Lid, T1.MyString, T1.Did, T1.Secid, T2.RowId
FROM Test1 as T1 INNER JOIN Test2 as T2 ON T1.Secid = T2.SecId AND T1.Did = T2.Did AND T1.Id = T2.RowId;
