Legend1989 Legend1989 - 3 months ago 7
SQL Question

Search SQL and return true or false

I have a table that has thousands of rows in. I need to check if certain values exists in the table or not.

I want to list all the bar codes I am searching with a flag of true or false returned if there is one.

I have come up with this so far:

SELECT CASE WHEN EXISTS (
SELECT *
FROM TABLE
WHERE Coulmn in ('a','b', 'c', 'd', 'e', 'f', 'g')
)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END


This however just returns a value of 1.

So in the table I have

coulmn
----------
A
B
D
E
F
G


I want to do a search that returns the following

Coulmn | Exsists
-----------------
A | True
B | True
C | False
D | True
E | True
F | True
G | True

Answer

With the help form above I created a temp table and then implemented one of the soultions shared.

CREATE TABLE #Temp
(
       Barcode VARCHAR (100)
)
INSERT INTO #Temp
VALUES
(1),
(2),
(3),
(4 )
select barcode, case when exists 
    (select * from CIPKORHHTProductDetails where CIPKORHHTProductDetails.Barcode = #temp.barcode)
    then 'True' else 'False' end as ExistsStatus
from #temp order by ExistsStatus DESC
Comments