Logan
Logan

Reputation: 293

Remove zeros from multiple columns

I have a table with ID, Numerator and Denominator columns.

How would I go about removing zeros when present in the same row for numerator and denominator?

I'm thinking I can do a case when statement, for example, case when Numerator = '0' and Denominator = '0' then remove, but my syntax is off.

Input table example

ID Numerator Denominator
------------------------
1      1         0
2      0         0

Output table example:

ID Numerator Denominator
------------------------
1      1         0
2                          <--- In SQL Server, it would possibly show up as NULL since it is blank data.

Upvotes: 0

Views: 170

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269663

You would use two case expressions:

select t.*,
       (case when numerator <> 0 or denominator <> 0 then numerator end) as new_numerator,
       (case when numerator <> 0 or denominator <> 0 then denominator end) as new_denominator
from t;

Upvotes: 2

Related Questions