AND and OR Operator on single table

  • Hi All,
    I have a dynamic where clause which needs to be applied on a table.
    where clause conditions explained below:
    1. where clause may contain one or more columns.
    2. where clause may contain both AND and OR Operator.
    3. depending on the user selection from the UI, where clause is formed.
    sample where clause as show below.

    Declare @Where varchar(1000) = '(Brand = ''Honeywell'' AND Brand = ''MCDonald'') AND _Status = 1 AND (_State = ''Florida'' AND _State = ''Georgia'')'

    Declare @Where1 varchar(1000) = '(Brand = ''Honeywell'' AND Brand = ''MCDonald'') OR _Status = 1 OR (_State = ''Florida'' AND _State = ''Georgia'') AND (Company_Type =''Y'')'

    so how to apply this where clause to the query to get the results from the table as attached in the file.


  • /* Result for this query should be - 'ABC'
     This can be done as follows*/
    select Distinct CompanyName
    from #Comapanytemp
    where (
            (Brand = 'Honeywell' AND _State = 'Florida'AND _status=1)
      OR (Brand = 'MCDonald' AND _State = 'Georgia' AND _status=1)
         )

    /* There is no MCDonald in the _State='Georgia'?. This part is confusing, why do you want the query to show an o/p of OUI
     Result for this query should be - 'ABC' and 'OUI'
    Not able to understand the question here.
    */

    select Distinct CompanyName
      from #Comapanytemp
     where (Brand = 'Honeywell' AND Brand = 'MCDonald')
       AND (_Status = 1 OR _Status = 0)
       AND (_State = 'Florida' AND _State = 'Georgia')

    --Result for this query should be - 'OUI' and 'NHU'
    --This can be done as follows

    select Distinct CompanyName
    from #Comapanytemp
    where (
            (Brand = 'Costco' AND _status=0
      OR (Brand = 'MCDonald' AND _Status = 0)
         )

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply