0

I've the following table

|ID | NodeID |
|---|--------|
|1  | N001   |
|2  | N006   |
|3  | N0010  |
|4  | N0015  |
|5  | N0016  |

And the following CSV string

'N001','N002','N003','N004','N005','N006','N007'

How can i find the NodeIDs that are present IN the CSV string and are NOT IN the table?

Mithun Sreedharan
  • 45,549
  • 69
  • 171
  • 232

1 Answers1

1

There are several options depending on your environment and data size. Here are my most common two...

The Way I Prefer...

Insert the CSV values into a table, then do all the work using SQL...

INSERT INTO myTempTable (itemValue) ......

SELECT * FROM myTempTable WHERE NOT EXISTS (SELECT * FROM dataTable WHERE NodeID = itemValue)
-- Or...
SELECT * FROM myTempTable LEFT JOIN dataTable ON NodeID = itemValue WHERE NodeID IS NULL


Another option avoids the temp table, but requires client side processing...

  1. Ensure the CSV is alpha-numerically ordered.
  2. Select all NodeIDs that ARE in the CSV, alpha-numerically ordered.
  3. Filter out the values found in step 2, from the original set of values.

Step 2 can simply be done as...

SELECT * FROM dataTable WHERE NodeID IN (yourCSV) ORDER BY NodeID
MatBailie
  • 70,516
  • 16
  • 91
  • 126