Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Append to WHERE clause on condition

Append to WHERE clause on condition

Scheduled Pinned Locked Moved Database
salesquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phillip Donegan
    wrote on last edited by
    #1

    Hi, Is it possible to append more conditions to a WHERE clause dependant on a condition? This is what i'm trying to do at the moment with no joy. E.G. ... WHERE (@extOrderID IS NULL OR Orders.ExtOrderID = @extOrderID) AND (@orderDateAfter IS NULL AND @orderDateBefore IS NULL OR Orders.TimeOrdered >= @orderDateAfter AND Orders.TimeOrdered <= @orderDateBefore) AND (@requiredDispatchDateAfter IS NULL AND @requiredDispatchDateBefore IS NULL OR (DATEADD(day, (SELECT SLA FROM Product WHERE Product.ID = OrderItem.ProductID), Orders.TimeOrdered)) >= @requiredDispatchDateAfter AND (DATEADD(day, (SELECT SLA FROM Product WHERE Product.ID = OrderItem.ProductID), Orders.TimeOrdered)) <= @requiredDispatchDateBefore) AND (@customerForename IS NULL OR Customer.Forename LIKE @customerForename) AND (@customerSurname IS NULL OR Customer.Surname LIKE @customerSurname) AND (@customerPostCode IS NULL OR Address.PostCode LIKE @customerPostCode) AND CASE WHEN @orderStatus IS NOT NULL THEN CASE WHEN @statusChecked = 'False' AND Status.StatusName != @orderStatus THEN 1 WHEN @statusChecked = 'True' AND Status.StatusName = @orderStatus THEN 1 ELSE 0 END END <> 0 AND CASE WHEN @isSearch = 1 THEN Orders.IsActive = 1 END It is the last CASE which is not working, any suggestions would be appreciated! Phil

    R 1 Reply Last reply
    0
    • P Phillip Donegan

      Hi, Is it possible to append more conditions to a WHERE clause dependant on a condition? This is what i'm trying to do at the moment with no joy. E.G. ... WHERE (@extOrderID IS NULL OR Orders.ExtOrderID = @extOrderID) AND (@orderDateAfter IS NULL AND @orderDateBefore IS NULL OR Orders.TimeOrdered >= @orderDateAfter AND Orders.TimeOrdered <= @orderDateBefore) AND (@requiredDispatchDateAfter IS NULL AND @requiredDispatchDateBefore IS NULL OR (DATEADD(day, (SELECT SLA FROM Product WHERE Product.ID = OrderItem.ProductID), Orders.TimeOrdered)) >= @requiredDispatchDateAfter AND (DATEADD(day, (SELECT SLA FROM Product WHERE Product.ID = OrderItem.ProductID), Orders.TimeOrdered)) <= @requiredDispatchDateBefore) AND (@customerForename IS NULL OR Customer.Forename LIKE @customerForename) AND (@customerSurname IS NULL OR Customer.Surname LIKE @customerSurname) AND (@customerPostCode IS NULL OR Address.PostCode LIKE @customerPostCode) AND CASE WHEN @orderStatus IS NOT NULL THEN CASE WHEN @statusChecked = 'False' AND Status.StatusName != @orderStatus THEN 1 WHEN @statusChecked = 'True' AND Status.StatusName = @orderStatus THEN 1 ELSE 0 END END <> 0 AND CASE WHEN @isSearch = 1 THEN Orders.IsActive = 1 END It is the last CASE which is not working, any suggestions would be appreciated! Phil

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Are you meaning that when @isSearch = 1 then Order.IsActive must equal 1 to return the row? If so, no you can't do that, but you could do this

      AND (@isSearch = 0 or Orders.IsActive = 1)

      Regards, Rob Philpott.

      P 1 Reply Last reply
      0
      • R Rob Philpott

        Are you meaning that when @isSearch = 1 then Order.IsActive must equal 1 to return the row? If so, no you can't do that, but you could do this

        AND (@isSearch = 0 or Orders.IsActive = 1)

        Regards, Rob Philpott.

        P Offline
        P Offline
        Phillip Donegan
        wrote on last edited by
        #3

        Rob Philpott wrote:

        Are you meaning that when @isSearch = 1 then Order.IsActive must equal 1 to return the row?

        Yes that is what I meant, can you think of possibly another way then of how I could dynamically create my where clause based on the parameters passed. I have found away to do this but it involves using an IF to see if @isSearch = 0 and then having the same query but with Order.IsActive = 1 on the end of one of them. Obviously I'd prefer to not do this as I'm just repeating code.

        R M 2 Replies Last reply
        0
        • P Phillip Donegan

          Rob Philpott wrote:

          Are you meaning that when @isSearch = 1 then Order.IsActive must equal 1 to return the row?

          Yes that is what I meant, can you think of possibly another way then of how I could dynamically create my where clause based on the parameters passed. I have found away to do this but it involves using an IF to see if @isSearch = 0 and then having the same query but with Order.IsActive = 1 on the end of one of them. Obviously I'd prefer to not do this as I'm just repeating code.

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          The clause in my previous post will do it.

          Regards, Rob Philpott.

          1 Reply Last reply
          0
          • P Phillip Donegan

            Rob Philpott wrote:

            Are you meaning that when @isSearch = 1 then Order.IsActive must equal 1 to return the row?

            Yes that is what I meant, can you think of possibly another way then of how I could dynamically create my where clause based on the parameters passed. I have found away to do this but it involves using an IF to see if @isSearch = 0 and then having the same query but with Order.IsActive = 1 on the end of one of them. Obviously I'd prefer to not do this as I'm just repeating code.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            The only reaslistic alternative is to use dynamic SQL (sql strings) but that is plain ugly. Also chaining multiple conditionals can nail a query to the wall. What I often do is use the "required" conditions to get a subset into a temp table and then start deleting records from the temp table or use the conditional filtering on the temp table

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups