Results 1 to 15 of 15

Thread: [RESOLVED] CASE Statment in SQL WHERE Clause

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] CASE Statment in SQL WHERE Clause

    I have the following SQL String:

    VB Code:
    1. Declare @UNIT NVARCHAR(50)
    2. Declare @CASENUM NVARCHAR(50)
    3.  
    4. SET @UNIT = Null /*'Sales'*/
    5. Set @CASENUM = Null /*'C0020'*/
    6.  
    7. SELECT INC.Unit, INC.CASENUM
    8. FROM INCIDENTS INC INNER JOIN CASES CS ON INC.CASENUM = CS.CASENUM
    9. WHERE
    10.        (CASE @CASENUM
    11.         WHEN Null THEN 1
    12.         WHEN @CASENUM THEN (CASE WHEN INC.CASENUM = @CASENUM THEN 1 ELSE 0 END) END) = 1
    13.     OR
    14.        (CASE @UNIT
    15.         WHEN Null THEN 1
    16.         WHEN @UNIT THEN (CASE WHEN INC.UNIT = @UNIT THEN 1 ELSE 0 END) END) = 1
    17. ORDER BY INC.UNIT

    This code works fine as long as I supply at least one of the Variables, but What I want to do if both variables are null I want all of the records returned. When both variables are omitted this SQL String returns no records.

    Any ideas?
    Last edited by Mark Gambo; Nov 27th, 2006 at 09:38 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: CASE Statment in SQL WHERE Clause

    Doesn't this do the same thing and handle the "both supplied situation?

    Code:
    Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
        and (@InvUnit is Null or @InvUnit=Inc.Unit)
    btw - what's with @InvUnit and @Unit?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE Statment in SQL WHERE Clause

    Quote Originally Posted by szlamany
    . . .btw - what's with @InvUnit and @Unit?
    Thanks again, Just a typo @InvUnit is really @Unit in disguise
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE Statment in SQL WHERE Clause

    Steve I forgot one statement in the sql string, if the @Unit is not Null I want all records that equal the @UNIT variable and that rows Status Column doesn't contain 'Completed'. If the @Unit is null I want all of the records returned based upon

    VB Code:
    1. Declare @UNIT NVARCHAR(50)
    2. Declare @CASENUM NVARCHAR(50)
    3.  
    4. SET @UNIT = Null /*'Sales'*/
    5. Set @CASENUM = Null /*'C0020'*/
    6.  
    7. SELECT INC.Unit, INC.CASENUM
    8. FROM INCIDENTS INC INNER JOIN CASES CS ON INC.CASENUM = CS.CASENUM
    9. WHERE
    10.        (CASE @CASENUM
    11.         WHEN Null THEN 1
    12.         WHEN @CASENUM THEN (CASE WHEN INC.CASENUM = @CASENUM THEN 1 ELSE 0 END) END) = 1
    13.     OR
    14.        (CASE @UNIT
    15.         WHEN Null THEN 1
    16.         WHEN @UNIT THEN (CASE WHEN (INC.UNIT = @UNIT AND [B]INC.STATUS <> 'Completed'[/B]) THEN 1 ELSE 0 END) END) = 1
    17. ORDER BY INC.UNIT
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: CASE Statment in SQL WHERE Clause

    So would this do it?

    Code:
    Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
        and (@Unit is Null or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE Statement in SQL WHERE Clause

    No dice, I am getting all the records irregardless of each records Status type
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: CASE Statement in SQL WHERE Clause

    Quote Originally Posted by Mark Gambo
    No dice, I am getting all the records irregardless of each records Status type
    When you specify @CaseNum or @Unit?

    btw - the problem with your WHERE/CASE block logic might be because you are missing ELSE statements in the outer-CASE. Without an ELSE a CASE/block will return a null if conditions are not met...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE Statement in SQL WHERE Clause

    Quote Originally Posted by szlamany
    When you specify @CaseNum or @Unit?
    When I don't specify either of these variables (Null)

    Quote Originally Posted by szlamany
    btw - the problem with your WHERE/CASE block logic might be because you are missing ELSE statements in the outer-CASE. Without an ELSE a CASE/block will return a null if conditions are not met...
    Hmm, I'll take a look at that, thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: CASE Statment in SQL WHERE Clause

    Code:
    Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
        and (@Unit is Null or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))
        and (@CaseNum is not null or @Unit is not null)
    So adding this third AND condition - that either of the two must not be null - should take care of that situation...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: CASE Statment in SQL WHERE Clause

    Small change to you code:

    VB Code:
    1. Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
    2.     and ((@Unit is Null and INC.STATUS <> 'Completed') or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))

    Thanks again Steve, you are a miracle worker
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] CASE Statment in SQL WHERE Clause

    BTW Steve:



    Sorry
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] CASE Statment in SQL WHERE Clause

    Glad it worked for you!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] CASE Statment in SQL WHERE Clause

    Nope spoke to soon, I came up with this workaround until I can figure it out:

    VB Code:
    1. Declare @UNIT NVARCHAR(50)
    2. Declare @CASENUM NVARCHAR(50)
    3.  
    4. SET @UNIT =  Null --'Group 32'
    5. Set @CASENUM = Null --'C01-0020'
    6.  
    7. IF @CASENUM is Not Null
    8.     SELECT INC.Unit, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
    9.     FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
    10.     INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
    11.     Where Inc.CaseNum = @CaseNum
    12.         ORDER BY INC.UNIT
    13.  
    14. IF @UNIT is Not Null
    15.     SELECT INC.Unit, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
    16.     FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
    17.     INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
    18.     Where Inc.Unit = @UNIT and INC.STATUS <> 'Completed'
    19.         ORDER BY INC.UNIT
    20.  
    21. IF @CASENUM is Null AND  @UNIT is Null
    22.     SELECT INC.UNIT, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
    23.     FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
    24.     INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
    25.     Where INC.STATUS <> 'Completed'
    26.         ORDER BY INC.UNIT

    A little cumbersome but it works for me
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] CASE Statment in SQL WHERE Clause

    Mark - that method has it's benefits...

    The QUERY OPTIMIZER gets a much simpler WHERE clause to analyze.

    It's probably easier to maintain in the future.

    There is the downside of having to keep the three select's the same - but otherwise...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] CASE Statment in SQL WHERE Clause

    Thanks for your help again Steve.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width