0

I have two columns, let's say col1 and col2. both of them is float

It's always if col1 = 0 then col2 > 0 or col1 > 0 then col2 = 0

I want to select only one column which is greater than zero.

Thanks in advance

DineshDB
  • 5,998
  • 7
  • 33
  • 49
Ugur
  • 23
  • 1
  • 1
  • Possible duplicate of [How do I compare two columns for equality in SQL Server?](https://stackoverflow.com/questions/1632792/how-do-i-compare-two-columns-for-equality-in-sql-server) – Brien Foss Jan 23 '18 at 05:36
  • Possible duplicate of [SQL Conditional column data return in a select statement](https://stackoverflow.com/questions/9290994/sql-conditional-column-data-return-in-a-select-statement) – Circle Hsiao Jan 23 '18 at 05:42
  • Sounds like your statement is: If `col1` equals zero, then `col2` is always greater than 0, so I would want to select `col2`. If `col2` equals zero, then `col1` is always greater than 0, so I would want to select `col1`. If this is true, then @DineshDB has the correct Answer. @Mischa Answer will provide the exact same result however since one or the other is always zero. N+0=N – Brien Foss Jan 23 '18 at 05:42

4 Answers4

1

Try this:

SELECT CASE WHEN Col1=0 THEN COL2 ELSE Col1 END
DineshDB
  • 5,998
  • 7
  • 33
  • 49
0

One of the two is always zero?

SELECT col1+col2

Mischa
  • 2,240
  • 20
  • 18
0

You can also use this :

SELECT CASE WHEN Col1 > 0 THEN COL1 ELSE Col2 END from your_table
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
0

You can use this for linq

from t in Table
select new { Col = ( t.col1  > 0 )?t.col1 : t.col2  }
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44