1
Answer

SQL query to select all items from a table based on WHERE condition in

Marius Vasile

Marius Vasile

Jul 12
357
1

Basicaly, I want to select all items in tbl1 WHERE RANGE_DEP in tbl2 match RANGE in tbl1

tbl1

ID COD PROCEDURE RANGE
1 S1 Procedure 1 MM,ME,MG,TF,CH
2 S2 Procedure 2 ME,MG,CH
3 S3 Procedure 3 MM,MG,TF

tbl2

ID DEPARTMENT RANGE_DEP
1 D1 MM,ME,CH
2 D2 TF,CH

I tried with 

Declare @Range nvarchar(100) = SELECT RANGE FROM tbl1

SELECT * FROM tbl2 WHERE (SELECT RANGE_DEP FROM tbl1 WHERE DEPARTMENT = D1) IN (SELECT * FROM STRING_SPLIT(@Range, ','))

but I am pretty sure is not the corect way. Can you help, please?

Answers (1)