views:

49

answers:

2

I'd like a SELECT query to return specific value if the count of a nested subquery is 0 ...

SELECT
  ( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
    FROM List
    WHERE Status = 1
      AND Deleted = 1
  ) AS Status

This does not work what is wrong with this syntax?

+3  A: 

I believe you want a case statement that looks like this:

CASE
 WHEN EXISTS(SELECT * FROM list WHERE Status = 1 AND Deleted = 1) THEN 'FALSE'
 ELSE 'TRUE'
END
Will A
If you like this answer then accept it - it only takes a few moments and having a non-0% acceptance rate will do you a lot of favours on SO.
Will A
This is great thanks alot!
A: 

Your query is missing the WHEN keyword in the CASE statement.

SELECT
  ( SELECT (CASE WHEN COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
    FROM List
    WHERE Status = 1
      AND Deleted = 1
  ) AS Status

You can go with that or you can simplify the query by removing the outer SELECT statement. And, the parentheses are optional too.

SELECT CASE WHEN COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END
FROM List
WHERE Status = 1
  AND Deleted = 1
bobs