Is there a reason why you cannot select an operator as a bit in SQL Server?

I am wondering why the following:

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 in accordance with the answer to this question , the conversion table says that an implicit conversion should occur.

+5
source share
4 answers

There is no Boolean data type in SQL BIT- this is a hack, but the main problem is that due to the SQL concept NULLtrue logical logic is impossible (for example, what would your query be returned if there pricewas NULL?)

, , , " " (, , TRUE OR NULL NULL - ), , SQL (, , TRUE OR NULL - TRUE, TRUE OR <anything> - TRUE).

(=, < =, >= ..) ( , WHERE CASE), .

+6

, , , IsCheap do SELECT * FROM STUFF WHERE IsCheap, WHERE IsCheap=1.

, , bool. , , , , - 0 1, bool . , , SQL , true/false 1/0.

+3

price < 500 : TRUE, FALSE UNKNOWN. , CASE .


FWIW Microsoft Database Database Engine , . , :

SELECT 1 = 1, 1 = NULL, 1 <> NULL, 1 IN (NULL)
  FROM Foo;

... , , , , Access SQL!

+2

MSSQL, Oracle. , Boolean . , , , - .

@paxdiablo that the point is so lacking ... The OP example is just a minimal example. This is still simplistic, but a real example. Consider the People table containing names and ages. You want to get all the people, but also want to know if they are minors. In MySQL and PostgreSQL you can write

SELECT name, age < 18 AS minor FROM people
0
source

All Articles