-
Does it give you what you are looking for?
DECLARE @t TABLE (a BIT, b BIT, c BIT)
INSERT INTO @t (a, b, c) SELECT 1, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 1, 1
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 0, 0
INSERT INTO @t (a, b, c) SELECT 0, 1, 0
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
DECLARE @a BIT, @b BIT, @c BIT
-- Try changing this to the desired combination
SELECT @a = 1, @b = 1, @c = 1
SELECT *
FROM @t
WHERE
(@a = 1 AND a = 1)
OR
(@b = 1 AND b = 1)
OR
(@c = 1 AND c = 1)
commented on Jul 4 2012 6:15AM
|
-
Hi Jacob Thanks for your reply
I need to get all the records if i pass @a=0 @b=0 and @c=0 irrespetive of condition. From the above query if i pass all zeros then i am not getting any rows.
commented on Jul 5 2012 2:17AM
|
-
How about this?
DECLARE @t TABLE (a BIT, b BIT, c BIT)
INSERT INTO @t (a, b, c) SELECT 1, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 1, 1
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 0, 0
INSERT INTO @t (a, b, c) SELECT 0, 1, 0
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
DECLARE @a BIT, @b BIT, @c BIT
-- Try changing this to the desired combination
SELECT @a = 0, @b = 0, @c = 0
SELECT *
FROM @t
WHERE
(@a = 0 OR a = @a)
AND
(@b = 0 OR b = @b)
AND
(@c = 0 OR c = @c)
commented on Jul 5 2012 2:38AM
|
-
If i keep AND there then the below scenarios wont work
If i receive @a value as 1 then i need to retrieve records having a=1 records only if i receive @a=1,@c=0 and @b=1 then i need to retrieve records having a=1 or b=1 if i receive @a=1,@b=0 and @c=1 then i need to retrieve records having a=1 or c=1
commented on Jul 5 2012 3:05AM
|
-
Is it closer to what you are looking for?
DECLARE @t TABLE (a BIT, b BIT, c BIT)
INSERT INTO @t (a, b, c) SELECT 1, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 1, 1
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
INSERT INTO @t (a, b, c) SELECT 1, 0, 0
INSERT INTO @t (a, b, c) SELECT 0, 1, 0
INSERT INTO @t (a, b, c) SELECT 0, 0, 1
DECLARE @a BIT, @b BIT, @c BIT
-- Try changing this to the desired combination
SELECT @a = 1, @b = 0, @c = 0
SELECT *
FROM @t
WHERE
(@a = 0 OR (@a = 1 AND a = @a))
AND
(@b = 0 OR (@b = 1 AND b = @b))
AND
(@c = 0 OR (@c = 1 AND c = @c))
commented on Jul 5 2012 3:21AM
|
-
If i am passing @a=1,@b=0 and @c=1 then
i am expecting records like this
a=1 or c=1
but i am getting like this
a=1 and c=1
commented on Jul 5 2012 4:17AM
|
-
Try this query
SELECT *
FROM @t WHERE
a = CASE WHEN @a=1 OR ( @a=0 AND @b=0 AND @c=0) THEN 1 ELSE NULL END OR
b = CASE WHEN @b=1 OR ( @a=0 AND @b=0 AND @c=0) THEN 1 ELSE NULL END OR
c = CASE WHEN @c=1 OR ( @a=0 AND @b=0 AND @c=0) THEN 1 ELSE NULL END
commented on Jul 5 2012 4:20AM
|
-
Or Try Another Simplest
SELECT * FROM @t
WHERE (1= CASE WHEN @a=0 AND @b=0 AND @c=0 THEN 1 ELSE NULL END) OR
(a = CASE WHEN @a=1 THEN 1 ELSE NULL END) OR
(b = CASE WHEN @b=1 THEN 1 ELSE NULL END) OR
(c = CASE WHEN @c=1 THEN 1 ELSE NULL END)
commented on Jul 5 2012 4:29AM
|
-
Thank you Mitesh, It is working.
commented on Jul 5 2012 4:30AM
|
-
Please Mark this as answer if it is working. so this question is closed.
commented on Jul 5 2012 4:51AM
|
-
commented on Jul 5 2012 8:31AM
|
|