5

For example:

declare @bitHaveRows bit
 select @bitHaveRows = count(*)
   from table
  where (predicate)

Are there any functions I can call on this line:

select @bitHaveRows = count(*)

to assign this bit a value of 0 if there are no rows, or 1 if there are one or more rows?

1
  • 2
    What db engine (mysql, oracle, microsoft)? Commented May 18, 2009 at 21:12

3 Answers 3

7

According to the conversion chart, there's an implicit conversion from int to bit. But if for some reason that doesn't work:

CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I needed. Thanks. I was hoping the implicit conversion would work in that context but apparently not.
7
declare @bRowsExist
SELECT @bRowsExist = CAST(count(*) as bit)
FROM yourtable

...not sure if it's a better query than the other suggestions

Comments

1

If your database supports it, you can use a CASE statement:

declare @bitHaveRows
select @bitHaveRows = case when count(*) > 0 then 1 else 0 end
from yourtable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.