SQL – Return TRUE/FALSE if query rows returned

Here’s a little trick you can use to return TRUE/FALSE if a query has returned results.

For example if a Person is in Category_ID 325 or 326 we want TRUE, otherwise FALSE.

This trick uses the COUNT function to check for any returned rows then a CASE statement and CAST to return either TRUE or FALSE

SELECT
CASE WHEN COUNT( Person_ID ) >= 1 THEN 
     CAST( 1 as BIT ) 
ELSE 
      CAST( 0 as BIT )  
END As IsPersonCat
FROM T_Persons
WHERE Category_ID IN ( 325, 326 )
Tags:

Was this article helpful?

Related Articles