Costa
Costa

Reputation: 4085

Can TSQL convert from boolean to BIT

I was creating a function that returns BIT, I tried to "Return @count < 1", that did not work, how to convert boolean to BIT in TSQL.

Thanks

Upvotes: 3

Views: 1339

Answers (3)

HABO
HABO

Reputation: 15816

Or a simple transmogrification of Shark's answer:

return case
    when @Count < 1 then 1
    else 0
    end

Note that a CASE may have as many WHEN clauses as you need.

Trivia: Curiously, a BIT can be set to 'TRUE' or 'FALSE'. Yeah, quoted strings. Go figure.

Upvotes: 1

gbn
gbn

Reputation: 432210

Can count ever be negative? And count should be integer

So what you want is "1 if @COUNT = zero, zero otherwise"

RETURN 1 - SIGN(@COUNT)

Upvotes: 3

user596075
user596075

Reputation:

You will need to have a conditional statement:

if @count < 1
    return 1
else
    return 0

Or you could use a CASE statement:

case
    when @count < 1 then return 1
    else return 0
end

Upvotes: 3

Related Questions