Getting Started with Adobe After Effects - Part 6: Motion Blur
Ask
Ask questions, discuss or help others by answering
Related Posts · View All
SQL Server 141
TSQL 75
SSRS 70
SSIS 66
XML 54

Top Categories · View All
SQL Server 141
TSQL 75
SSRS 70
SSIS 66
XML 54

How to compare three bit column values in a single table

Jul 4 2012 12:00AM by mailtokvm   

I have 3 bit columns in my database a, b, c and these are my input parameters(@a,@b and @c) to StoredProcedure coming from UI. If value is not passed then consider default values as 0. Now i need deatils to display like this

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
...
...
...
if i receive @a=0,@b=0 and @c=0 then i dont need to consider any condition.

Submitted under: Microsoft SQL Server · TSQL ·  ·  · 


mailtokvm
355 · 0% · 115

11 Replies

  • 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
    Jacob Sebastian
    1 · 100% · 32002
  • 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
    mailtokvm
    355 · 0% · 115
  • 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
    Jacob Sebastian
    1 · 100% · 32002
  • 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
    mailtokvm
    355 · 0% · 115
  • 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
    Jacob Sebastian
    1 · 100% · 32002
  • 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
    mailtokvm
    355 · 0% · 115
  • 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
    Mitesh Modi
    18 · 10% · 3078
  • 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
    Mitesh Modi
    18 · 10% · 3078
  • Thank you Mitesh, It is working.

    commented on Jul 5 2012 4:30AM
    mailtokvm
    355 · 0% · 115
  • Please Mark this as answer if it is working. so this question is closed.

    commented on Jul 5 2012 4:51AM
    Mitesh Modi
    18 · 10% · 3078
  • good job mitesh!

    commented on Jul 5 2012 8:31AM
    Jacob Sebastian
    1 · 100% · 32002

Your Reply


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]