October 22, 2024
Chicago 12, Melborne City, USA
SQL

How to select rows with atleast one non-zero value in SQL server


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

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video