0

I need to check if a row is zero. For nulls I usually use isnull() but my rows are never null. If there is such a command, I would use it like this:

select * from maintable where column1 = iszero(@param,column1)
user999690
  • 257
  • 4
  • 10
  • 23

3 Answers3

1

Assuming your looking for something to work in the same way as IsNull() you could define your own function

create function IsZero(@value int, @default int)
    returns int
as
begin

    if (@value <> 0)
        return @value

    return @default
end

Example of usage

select 
    Returns5 = dbo.Iszero(5, 10),
    Returns10 = dbo.Iszero(0, 10)

or in your case

select * from maintable where column1 = IsZero(@param,column1)
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
0
SELECT * 
FROM   maintable 
WHERE  column1 = CASE @param 
                     WHEN 0 THEN column1 
                     ELSE @param 
                 END

This will give you the record in the maintable with where column1 equals the @param variable. If the @param variable is 0 then it will return all records in the maintable.

XN16
  • 5,679
  • 15
  • 48
  • 72
0

Try this:

select *
from maintable
where column1 = @param or @param = 0 
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786