A table has CSV data and I want to do a SELECT DISTINCT ID
on that table, how should I proceed?
+-----------+
| id |
+-----------+
| 111,1487 |
+-----------+
| 462 |
+-----------+
| 2492 |
+-----------+
| 3184 |
+-----------+
| 3181,3184 |
+-----------+
| 3181 |
+-----------+
| 440 |
+-----------+
| 1436 |
+-----------+
I've got a function which I usually use that can split varchar csv string of INTs into a one-column table which I use as follow:
SELECT number from INTLIST_TO_TBL('123,234,345,4566')
It works well but I can't use it in this case as there are many rows and it will return multiple rows in certain cases.
Should I try using a cursor that inserts into a temporary table then proceed to the next step by querying this temporary table ?
I've been told cursors are synonym of poor performance which is why I always resort to asking here first before going down that road.
Best Answer
You should be able to use CROSS APPLY.
In an effort to provide a Minimum Complete Verifiable Example, I've included a 'simple' SPLIT TVF which is intended to emulate your INTLIST_TO_TBL function. This is for demonstration purposes only and there are more efficient ways to split a string that are beyond my answer.
Now that we have the Split TVF, let's see an example of how to incorporate it.