tags:

views:

24

answers:

2

In a tsql query, I want to have a calculated field that is the boolean result of a string comparison.

It would look something like this:

select name, (status = 'current') as IsValid
from items

But the query as I have listed is not valid. What is the correct syntax?

+4  A: 

I would use a case statement

Select name, case when status = 'current' then 1 else 0 end as IsValid
from items
cmsjr
@cmsjr - beat me to it. just a few seconds ;)
Sachin Shanbhag
@Epotter - note that your "bool" you asked for is (correctly) represented in TSQL as a "bit,", i.e., 1 or 0. So test against a bit in your downstream code.
SethO
@Sachin, I might have had a few second advantage from using the approach earlier this morning ;) @SethO, solid comment, bit as boolean isn't necessarily obvious, thanks.
cmsjr
+3  A: 

Try this -

select name, 
(CASE status WHEN 'current' THEN 1 Else 0 END) as IsValid 
from items
Sachin Shanbhag