Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason why you cannot select a statement as a bit in SQL Server?

I am wondering why the following fails:

SELECT price<500 as PriceIsCheap

and forces you to do the following:

SELECT CASE WHEN (price<500) THEN 1 ELSE 0 END as PriceIsCheap

When, as per the answer to this related question, the conversion table says that an implicit conversion should occur.

like image 529
Matt Mitchell Avatar asked Jun 30 '10 05:06

Matt Mitchell


People also ask

How do I select a bit value in SQL?

You can use the CONVERT operator. CAST or CONVERT will work. Show activity on this post. 1 is the display for a true bit.

How do you write a bit in SQL?

If there are 8 or fewer bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on. The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.

What is the bit datatype in SQL Server?

SQL Server BIT data type is an integer data type that can take a value of 0, 1, or NULL . SQL Server optimizes storage of BIT columns. If a table has 8 or fewer bit columns, SQL Server stores them as 1 byte. If a table has 9 up to 16 bit columns, SQL Server stores them as 2 bytes, and so on.

How do I update a bit field in SQL?

1 Answer. Show activity on this post. Bits in SQL Server are always stored as 1 or 0 in a bitmap. The "Edit Table" option in SSMS just translates this to True or False for presentation purposes, this is nothing to do with how it is actually stored.


2 Answers

There is no boolean data type in SQL, BIT is kind of a hack, but the main problem is that due to the SQL concept of NULL true boolean logic is impossible (for example, what would your query return if price was NULL?)

Note that I'm not saying that there are not possible ways to implement boolean logic that "mostly" work (for example, you could say that TRUE OR NULL is NULL or whatever) just that the people who designed the SQL standard couldn't decide on The One True Representation for boolean logic (for example, you could also argue that TRUE OR NULL is TRUE, since TRUE OR <anything> is TRUE).

The boolean expressions (=, <=, >=, etc) are only valid in certain places (notably, WHERE clauses and CASE labels) and not in any other place.

like image 155
Dean Harding Avatar answered Oct 05 '22 05:10

Dean Harding


Well you'll also find you can't if you have a bit column called IsCheap do SELECT * FROM STUFF WHERE IsCheap, you have to do WHERE IsCheap=1.

The reason is simple, the data type is a bit, not a bool. True, it's basically the only use you'll put it to and it's implicitly converted by almost any data access framework, but it's still technically a bit with 0 or 1 rather than a bool with true or false. There's an obvious connection we can all see, but SQL wasn't written with this assumption in it so we have to provide the logic to convert true/false to 1/0.

like image 37
fyjham Avatar answered Oct 05 '22 05:10

fyjham