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)
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)
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)
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
.
Try this:
select *
from maintable
where column1 = @param or @param = 0