Gaslight Deceive Subvert
Gaslight Deceive Subvert

Reputation: 20354

Boolean expression as column value in transact sql

In most RDBMS:es, this work:

select (5 > 3)

and evaluates to true. It doesn't work in MS Transact SQL and the only workaround I've found is to write:

select case when 5 > 3 then 1 else 0 end

Which kind of sucks because it is much more verbose. Is there a better way to write the above kind of checks?

Upvotes: 7

Views: 5657

Answers (3)

Valentin Kuzub
Valentin Kuzub

Reputation: 12073

If your program often requires such case constructs you could create your set of functions that will have user functions like Bool_IsGreater(left, right) that will return you your desired 0 or 1.

SQL Server doesn't support boolean value type anyway even for basic column use.

If you will need performance and those 5 and 3 values come naturally from some select query you might want to create a new column and set its value to 1 or 0 by trigger or something, which could help with performance.

Upvotes: 0

Bill
Bill

Reputation: 4585

You could write it as a scalar-valued function, but it will be very slow on large datasets.

Upvotes: 0

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125204

If the problem is arithmetic comparison:

select (5 - 3)

Then at the application level test for < or = or > 0.

Upvotes: 2

Related Questions