0

I am writing a stored procedure where I want to select multiple IDs from one table where ids are found on another table from coma separated values (1,2,3,6).

This is what I am trying to write. But it is not working as expected.

SELECT [Name]  
FROM [Table1] t1 
INNER JOIN [Table2] t2 ON REPLACE(t1.[ID], ',', '') = t2.[ID]
orbnexus
  • 682
  • 8
  • 33
  • Procedural code like stored procedures is **highly vendor-specific** - so please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s Sep 22 '17 at 20:32

1 Answers1

0

This is the answer

SELECT [Name]
FROM [Table1] t1 
INNER JOIN [Table2] t2
ON ',' + t1.[ID] + ',' like '%,' + cast(t2.[ID] as nvarchar(20)) + ',%'
orbnexus
  • 682
  • 8
  • 33
  • No. This is a workaround. The correct answer would be to normalize the database and never ever store values as a comma-delimited text. – Zohar Peled Sep 24 '17 at 13:57