Avatar of BostonMA
BostonMA
Flag for United States of America

asked on 

Sql else statement in CASE cannot contain computed field of (column a / column b)

We have a reporting tool which lets us create  a computed column in SQL server 2000.

I want the column to be   Computer Column = Column A / Column B

Sounds simple, but I have the following requirements:

Column B can be zero, null, or a real number.
The SQL needs to be contained in a subselect (this is a requirement of our reporting tool).  

Here is an example of what I've tried.


select
(  
CASE
WHEN Sales.Column B = 0 THEN Sales.ColumnA
WHEN Sales.Column B IS NULL THEN Sales.ColumnA
ELSE (Sales.Column A / Sales.Column B)
END
)
from Sales

Column A      Column B      Computed Column
3      18      0
8      53      0
12      83      0
57      310      0
32      304      0
179      877      0
114      1174      0



However as you see I'm getting mainly zeros which doesnt make sense.  I can tell the statement is working a lttle because in the case where column B is zero, it is displaying column A.  Also when it encounters a null it is assigning the correct value.

Whats got my confused is when I try the following:

select
(
   
CASE
WHEN Sales.Column B = 0 THEN Sales.ColumnA
WHEN Sales.Column B IS NULL THEN Sales.ColumnA
ELSE 2
END
)
from Sales


It correctly assigns a number 2 when it should.   So my question is, why wont it do the dividing of two columns in my else statement?
SQL

Avatar of undefined
Last Comment
BostonMA

8/22/2022 - Mon