Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is my table:

TABLE 1
facility | CPTCode
1        | 45
2        | 46
3        |456,678


Now I have another table that reads the values from this table and generate another report. So for facility 1 and 2, its pretty straight forward

TABLE TEST
facility | CPTCode |Something
1        | 45      |Apple
2        | 46      |Orange
3        | 456     |Banana
3        | 678     |Strawberry

SQL
select * from TEST 
where facility = '1' and CPTCode = 45

but how do i write my query where it takes care on comma
The query should be like
SQL
Select * from TEST 
where facility = '3' and CPTCode in('456','678')

How do I write this in sql?
SQL
SELECT facility , TxnSerDate, TxnBchDate, TxnProcedure, TxnAmount, CPTCode 	
FROM TEST  
where facility = @facility  and CPTCode = @CptCode

Need help writing the
SQL
SELECT facility , TxnSerDate, TxnBchDate, TxnProcedure, TxnAmount, CPTCode 	
FROM TEST  
where facility = @facility  and CPTCode = @CptCode = @CPTCode statement


What I have tried:

SQL
SELECT facility , TxnSerDate, TxnBchDate, TxnProcedure, TxnAmount, CPTCode 	
FROM TEST  
where facility = @facility  and CPTCode = @CptCode
Posted
Updated 18-Apr-18 17:52pm
v3

Ok. Based on what presented. I'm guessing, there is a parameter like '456,678' in the Stored Procedure, and you want to put the parameter into the query and expect the query to return the rows with CPTCode 456 and 678? If the the case, here is an example.

SQL
DECLARE @temp TABLE  (
 Facility INT IDENTITY(1,1),
 CPTCode VARCHAR(30),
 Something VARCHAR(50)
)

INSERT INTO @temp
	SELECT 45, 'Apple' UNION
	SELECT 46, 'Orange' UNION
	SELECT 456, 'Pear' UNION
	SELECT 577, 'Banana' UNION
	SELECT 678, 'Strawberry'

	--parameter
DECLARE @CptCode  VARCHAR(30)
SET @CptCode  = '45,456,577'

--comma separated string into rows
DECLARE @cptCodeRows TABLE(CPTCode VARCHAR(30));
;WITH CTE_CPTCode
AS
(
    SELECT [xml_val] = CAST('<b>' + REPLACE(@CptCode ,',','</b><b>') + '</b>' AS XML)
)
INSERT INTO @cptCodeRows
	SELECT  
			ProgramId = col.value('.','VARCHAR(30)')
	FROM CTE_CPTCode
	CROSS APPLY [xml_val].nodes('/b') CA(col)

	--get all the records in @CptCode  
SELECT * FROM @temp t JOIN @cptCodeRows c
	ON t.CPTCode = c.CPTCode

Output:
Facility	CPTCode	  Something	CPTCode
1	        45	      Apple	    45
3	        456	      Pear	    456
4	        577	    Banana	    577
 
Share this answer
 
Instead of storing multiple foreign keys inside a single column, I would recommend adding a junction table where you store each key on an individual row. See Many-to-many (data model) - Wikipedia[^]

Using a junction table your query would simply be something like
SQL
SELECT ...
FROM Table1,
     JunctionTable1,
     Test
WHERE JunctionTable1.Facility = Table1.Facility
AND   JunctionTable1.CptCode = Test.CptCode
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900