Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Boolean result as query column?

Author  Topic 

Funger
Starting Member

6 Posts

Posted - 2005-07-11 : 15:39:05
Hi all,
I want the boolean result to be a column in a query.

For example, lets say I have a [Customer] table. I want to return all of the records in the [Customer] table, and a bit value for whether the CustomerID column is 123.

My first thought would be the following:

SELECT (CustomerID = 123) AS MyCheck FROM Customer

This, if you try it obviously doesn't work. If I try this query in EM, it changes the CustomerID = 123 into a string.

I know i can use an IF, or an CASE, but I was curious if there was a way to evaluate the statement directly.

Thanks for help in advance!

-Funger

[edit] I meant CASE not ELSE.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-07-11 : 15:40:53
You can use CASE in queries. Check SQL Server Books Online for details.

SELECT CustomerID, MyCheck = CASE WHEN CustomerID = 123 THEN 1 ELSE 0 END
FROM Customer

Tara
Go to Top of Page

Funger
Starting Member

6 Posts

Posted - 2005-07-11 : 15:43:35
quote:
Originally posted by tduggan

You can use CASE in queries. Check SQL Server Books Online for details.

Tara



I was looking for something other then an IF or a CASE statement.
I origionally came from MS Access, and was able to evaluate a boolean statement as a column rather easily, I thought there would be a SQL Server equivalent.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-07-11 : 15:45:27
I updated my post with an example.

I've never used Access before so I don't know what option you are referring to.

Tara
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-07-11 : 15:51:42
There is no exposed boolean datatype that you can use in SQL Server; the closest is a "bit" which is either 1 or 0. SQL uses booleans internally, of course, but you cannot declare variables or table columns of that type.

Expressions that return a boolean value cannot be mixed with other expression in SQL. You may notice that in Books On-Line, there are places in the syntax of variuos commands where a boolean-expression is required and others where an expression is required; there is no casting between the two types. Any expression containing any of the boolean operators (<,>,=,!=, IN, EXISTS, etc) is a boolean expression.

Usually, you'd use a CASE statement to "convert" boolean expressions back and forth to non-boolean expressions in T-SQL, as Tara has demonstrated.

- Jeff
Go to Top of Page

Funger
Starting Member

6 Posts

Posted - 2005-07-11 : 17:12:14
Thank you to both. :)
Go to Top of Page
   

- Advertisement -