Results 1 to 15 of 15

Thread: [RESOLVED] CASE Statment in SQL WHERE Clause

Threaded View

  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."


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