How can I join on a CSV varchar?

Posted by mgroves on Stack Overflow See other posts from Stack Overflow or by mgroves
Published on 2010-05-12T17:05:54Z Indexed on 2010/05/12 17:14 UTC
Read the original article Hit count: 243

Filed under:
|
|
|
|

I have a varchar field that contains a string like "10,11,12,13". How can I use that CSV string to join to another table with those IDs? Here's the approach I'm taking now:

select *
from SomeTable a
WHERE (',' + @csvString + ',') LIKE '%,' + CONVERT(varchar(25), a.ID) + ',%'

Where @csvString is "10,11,12,...". I intend to use this method as a join condition as well.

That method works, but it's rather slow (using CAST doesn't improve the speed).

I understand that having CSVs in the database like that is usually a very silly idea in most cases, but there's nothing I can do about that.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about mssql