Bharat Guru
asked on
how can we split and join the table
How to split by comma and join each value from table1 value field with table2 symbol
Table1(ID, Value)
1, [AB, BC, DF, TD]
2, [BC, AC, TD]
Table2(ID, Symbol)
1, AB
2, AC
3, BC
4, DF
5, TD
Table1(ID, Value)
1, [AB, BC, DF, TD]
2, [BC, AC, TD]
Table2(ID, Symbol)
1, AB
2, AC
3, BC
4, DF
5, TD
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you're using SQL Server 2016 or later you can utilize the newly added STRING_SPLIT() function instead of writing your own.
ASKER
getting error message
The multi-part identifier "t1.value" could not be bound.
The multi-part identifier "t1.value" could not be bound.
you might have to encapsulate the value in brackets eg:
t1.[value] as "value" is a reserved word in SQL.
t1.[value] as "value" is a reserved word in SQL.
ASKER
How can we split values only between open and close brackets [ ]. For example "This is a test [AB, BC, BD] test [EF, BD]"
AB
BC
BD
EF
BD
AB
BC
BD
EF
BD
You're going to have to write your own table-valued string-parsing function to extract the comma-delimited list then pass it to the string_split function.
Here is some code that might help...
Here is some code that might help...
DECLARE @Test VARCHAR(MAX) = 'This is a test [AB, BC, BD] test [EF, BD]'
DECLARE @List TABLE (Item VARCHAR(MAX));
WITH cte AS
(
SELECT @Test AS String,
CHARINDEX('[', @Test, 1) AS StartIndex,
CHARINDEX(']', @Test, 1) AS EndIndex,
SUBSTRING(@Test, CHARINDEX('[', @Test, 1) + 1, CHARINDEX(']', @Test, 1) - CHARINDEX('[', @Test, 1) - 1) AS List,
SUBSTRING(@Test, CHARINDEX(']', @Test, 1) + 1, LEN(@Test)) AS Remaining
UNION ALL
SELECT Remaining AS String,
CHARINDEX('[', Remaining, 1) AS StartIndex,
CHARINDEX(']', Remaining, 1) AS EndIndex,
SUBSTRING(Remaining, CHARINDEX('[', Remaining, 1) + 1, CHARINDEX(']', Remaining, 1) - CHARINDEX('[', Remaining, 1) - 1) AS List,
SUBSTRING(Remaining, CHARINDEX(']', Remaining, 1) + 1, LEN(Remaining)) AS Remaining
FROM cte
WHERE CHARINDEX('[', Remaining, 1) > 0
)
SELECT RTRIM(LTRIM(S.[value])) AS item
FROM cte
CROSS APPLY STRING_SPLIT(cte.List, ',') AS S
ASKER
Is above function will work for 'This is a test [AB, BC, BD] test [EF] test1 [BD]?
If you have an exe you don't need an installer. You can simply run the sc command:
sc create MyService binPath= "C:\Code\MyService\MyService.Exe" DisplayName= "My cool Service" start= auto
ASKER
Above code will not work when we don't open and close bracket
Select SUBSTRING('test', CHARINDEX('[', 'test', 1) + 1, CHARINDEX(']', 'test', 1) - CHARINDEX('[', 'test', 1) - 1)
Select SUBSTRING('test', CHARINDEX('[', 'test', 1) + 1, CHARINDEX(']', 'test', 1) - CHARINDEX('[', 'test', 1) - 1)
ASKER
Thanks for help
This will be output for table1 and make inner join on Table2 but remember to remove special character like '",","[","]" etc.
Your final query would be like,
Open in new window
It's just an idea. Please try to replicate on your end.
fnsplit.zip