Re: IIF in where clause to exclude selection criteri when null

From: Vishal Parkar (REMOVE_THIS_vgparkar_at_yahoo.co.in)
Date: 06/23/04

  • Next message: Nigel Rivett: "RE: Check to see if a table exists"
    Date: Wed, 23 Jun 2004 19:42:45 +0530
    
    

    hi baz,

    if i understand you correctly you are looking for query as follows, what
    this query will do is check the value of @acctnum variable, if it is 'all'
    then no condition for acctnum will be checked otherwise acctnum will be
    filtered out for the equivalent value of @acctnum variable

    Select * from a
    WHERE AcctNum = case when @AcctNum ='all' then AcctNum else @acctnum end
    and a.FISCYR + a.FISCPER <= @year + @period

    see similar following example on northwind database.
    --if @acctnum = 'all' retrieve all the rows from table customers

    declare @AcctNum varchar(50)
    set @AcctNum ='all'
    Select * from customers
    WHERE customerid = case when @AcctNum ='all' then customerid else @acctnum
    end

    --change value of @acctnum is specific customerid,
    -- and same query will retrieve releted rows from table customers

    set @AcctNum ='alfki'
    Select * from customers
    WHERE customerid = case when @AcctNum ='all' then customerid else @acctnum
    end

    --
    Vishal Parkar
    vgparkar@yahoo.co.in | vgparkar@hotmail.com
    

  • Next message: Nigel Rivett: "RE: Check to see if a table exists"

    Relevant Pages

    • Re: Sort Using A View
      ... select top 100 percent * from customers ... Rather use order by clause in the final query. ... Vishal Parkar ...
      (microsoft.public.sqlserver.programming)
    • Re: ORDER BY "RANDOM"
      ... use "order by newid()" ... select * from customers ... Vishal Parkar ... > I want to query a table but I want the resultset in a random order that's ...
      (microsoft.public.sqlserver.programming)