tags:

views:

96

answers:

4

i have a procedure in which the below condition is to be written in a WHERE clause. How do I do that using CASE, or something like it?

IF (@itemId IS NOT NULL)
  dataId = @itemid 
+2  A: 
Case When @ItemId Is Not Null And dataId = itemId Then 1 Else 0 End = 0

If dataId is not nullable, then you can simplify it to:

dataId = Coalesce(@ItemId, dataId) 

Or using a Case expression

dataId = Case When @ItemId Is Null Then itemId Else dataId End
Thomas
+3  A: 

You don't need a case for that:

... where @itemId is null or dataId = @itemId ...

Or:

... where isnull(@itemId, dataId) = dataId ...
Guffa
A: 

go on, read the msdn :)

otherwise eg.

SELECT *
FROM   [testTable]
WHERE  ISNULL(@itemId, [dataId]) = [dataId]

please elaborate your sepcificatons!

Andreas Niedermair
I think this one has been put to death and answering at this point with yet another Query response that doesn't meet the questions needs seems to be trivial.
Joe Garrett
lol ... now that's a real good reason for a downvote ... so then, why didn't you downvote the anser of guffa?
Andreas Niedermair
+1  A: 

You can do something like this:

SELECT dataId = CASE WHEN @itemId IS NOT NULL THEN @itemId 
        ELSE dataId END
 FROM YourTable
Darvis Lombardo