July 18, 2005 at 1:51 pm
I have have a parameter in a stored procedure which I wish to use in the WHERE clause of a SELECT statement as a filter, varCategory is an integer and can be null. Is anyone able to tell me if the pseudo code below is possible in T-SQL. ie is an IF or CASE statement permissable in a WHERE clause?
declare @varCategory int
Select * from tblparticipants
where
IF varCategory = NULL then
--ignore where clause
ELSE IF varCategory = 1 or 2 or 3 THEN
--filter on varCategory = @varCategory
END IF
July 18, 2005 at 1:55 pm
declare @varCategory int
Select * from tblparticipants
Where
varCategory = CASE WHEN @varCategory IS NULL THEN varCategory ELSE @varCategory END
Regards,
gova
July 18, 2005 at 6:25 pm
I just thought the query maybe much better without using if-else, CASE etc..
select *
from tblparticipants
where varCategory is null
union
select *
from tblparticipants
where varCategory = @varCategory
July 19, 2005 at 7:14 am
Try this:
select *
from tblparticipants
where varCategory = isnull(@varCategory, varCategory)
If the parm is null, the value from the current row is used, which will in effect just ignore null
July 19, 2005 at 7:18 am
That is a good one.
Regards,
gova
October 31, 2015 at 11:21 am
The near same "hydera" but I have 2 variables varCategory, varName and I need condition AND beetween
varCategory AND varName below:
declare @varCategory int
declare @varName varchar(50)
Select * from tblparticipants
where
IF varCategory = NULL then
--ignore where clause
IF varCategory = 1 or 2 or 3 THEN
--varCategory = @varCategory
IF varName = NULL then
--ignore where clause
IF varName != NULL then
AND varName = @varName (I mean, where clause varCategory AND varName)
END IF
In above, how must we do it? Please help me.
October 31, 2015 at 4:34 pm
childhood (10/31/2015)
The near same "hydera" but I have 2 variables varCategory, varName and I need condition AND beetweenvarCategory AND varName below:
declare @varCategory int
declare @varName varchar(50)
Select * from tblparticipants
where
IF varCategory = NULL then
--ignore where clause
IF varCategory = 1 or 2 or 3 THEN
--varCategory = @varCategory
IF varName = NULL then
--ignore where clause
IF varName != NULL then
AND varName = @varName (I mean, where clause varCategory AND varName)
END IF
In above, how must we do it? Please help me.
Gosh. This whole thread has been about how to do it. At least try.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply