5

Why the query below is not working? Doesn't a logical expression return a boolean/bitwise value?

SELECT @v1 = CAST( (@v2 > 0) AS INT)
FLash
  • 696
  • 9
  • 27
  • 10
    No, it does not. T-SQL has no boolean values, only boolean expressions. And yes, this is a stupid limitation. Use `SELECT @v1 = CAST(CASE WHEN @v2 > 0 THEN 1 ELSE 0 END AS BIT)` instead (assuming the cast is even necessary; it's not if `@v1` is already declared `BIT`, since implicit conversions take care of it). – Jeroen Mostert Feb 05 '19 at 13:26
  • I'm only partially convinced that this is a duplicate of [How to set bool value in SQL](https://stackoverflow.com/questions/41695064/how-to-set-bool-value-in-sql/41695199#41695199), So I will not close as a dupe. – Zohar Peled Feb 05 '19 at 13:29

1 Answers1

6

Following statement will do what you are trying to achieve.

SELECT @V1 = IIF ( @V2 > 0, 1, 0 ) 
PSK
  • 17,547
  • 5
  • 32
  • 43