OiO.lk Blog SQL Select rows with at least one non-zero value
SQL

Select rows with at least one non-zero value


I have a table with 20+ columns and some rows would only have 0s for all the column. Is there a way to filter out rows where all the columns have 0?

I could use

select * 
from table 
where concat(col1, col2, col3) != '000'

but I don’t want to write 20+ zeroes in my case and there is a possibility that the column count could increase in the future.

I could also use

select * 
from table 
where coalesce(col1, col2, col3) > 0 
   or coalesce(col1, col2, col3) < 0 

as some values could be negative. But I don’t want to write 20+ column names.

Is there a dynamic way to solve this issue?



You need to sign in to view this answers

Exit mobile version