Marcel H.
Marcel H.

Reputation: 111

SQL join on string_split values

So, I have a column value something like this:

1,15,32,64

And another table (called infoTable) where the identifier and text is in seperate columns. What would be the best way to get the Data of the infoTable into an select from the mainTable ?

I've thought about string_split but cant find anything on how i should start with that.

Im currently using MS SQL Server Manager Studio.

Upvotes: 0

Views: 2214

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270301

I think you are looking for cross apply:

select t.*, i.text
from maintable t cross apply
     string_split(t.ids, ',') s join
     infotable i 
     on i.id = s.id;

Upvotes: 2

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31991

below is the join

select i.* FROM infoTable i
INNER JOIN string_split('1,15,32,64',',') sp
on i.columname=sp.value

Upvotes: 0

Related Questions