0

I would like to use the result of a test mathematically to decide whether to set a value to a different number if it happens to be zero or whatever.

For example, if I issue something like this: SELECT (1=1)* 100 AS value I would like value to result as something like 100 or -100 (the latter is how I remember Commodore Basic working). But instead I get a syntax error on the equal sign. Is there a way to do this in MSSQL to simplify my queries?

  • 1
    Use CASE When [link](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-2017) – Hasan Mahmood Apr 15 '19 at 18:39
  • Possible duplicate of [SQL Server: Cast bool as integer](https://stackoverflow.com/questions/15056542/sql-server-cast-bool-as-integer) – GSerg Apr 15 '19 at 18:41

1 Answers1

1

You could use case when

SELECT (case when 1=1 then 1 else -1 end)* 100

Also you could use iif (Only for SQL Server 2012 or above version)

SELECT iif(1=1,1 ,-1)* 100
Yann39
  • 14,285
  • 11
  • 56
  • 84
Ahmed Qasid
  • 293
  • 2
  • 9