Home All Groups Group Topic Archive Search About
Author
1 Jul 2005 8:38 AM
Wayne Gibson
Hi,
Was wondering if somebody could tell me if it is possible to use a CASE
statement within in the WHERE clause.

I'm looking to create a store procedure that can a number of parameters.
Then depending on the parameters that were passed, define the correct WHERE
clause.

Many thanks..

Wayne

Author
1 Jul 2005 11:26 AM
Tom Moreau
CASE expressions in WHERE clauses can typically be collapsed to OR
predicates:

select
    *
from
    MyTable
where
       (@MyParm = 1)
or    (@MyParm = 2 and MyCol in (1, 2, 3))
or    (@MyParm = 3 and MyDate > '2005-07-01')

--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
..
"Wayne Gibson" <wayne.gib***@rendersoft.com> wrote in message
news:da2vem$8cs$1$830fa7b3@news.demon.co.uk...
Hi,
Was wondering if somebody could tell me if it is possible to use a CASE
statement within in the WHERE clause.

I'm looking to create a store procedure that can a number of parameters.
Then depending on the parameters that were passed, define the correct WHERE
clause.

Many thanks..

Wayne
Author
1 Jul 2005 12:51 PM
Wayne Gibson
Sorry I don't think that I explained it very well, I was hoping to use the
CASE statement like this...

SELECT * FROM Table WHERE
CASE @param1
    WHEN "1" THEN field1 = something
    WHEN "2" THEN field2 = something
END
Author
1 Jul 2005 2:56 PM
Jeremy Williams
See http://www.sommarskog.se/dyn-search.html for an excellent discussion of
your options.

Show quote
"Wayne Gibson" <wayne.gib***@rendersoft.com> wrote in message
news:da3e83$e7r$1$8302bc10@news.demon.co.uk...
> Sorry I don't think that I explained it very well, I was hoping to use the
> CASE statement like this...
>
> SELECT * FROM Table WHERE
> CASE @param1
>     WHEN "1" THEN field1 = something
>     WHEN "2" THEN field2 = something
>  END
>
>
Author
1 Jul 2005 3:12 PM
Tom Moreau
You can still use the OR predicates:

SELECT *
FROM Table
WHERE
      (@param1 = "1" AND field1 = something)
OR  (@param1 = "2" AND field2 = something)


--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
..
"Wayne Gibson" <wayne.gib***@rendersoft.com> wrote in message
news:da3e83$e7r$1$8302bc10@news.demon.co.uk...
Sorry I don't think that I explained it very well, I was hoping to use the
CASE statement like this...

SELECT * FROM Table WHERE
CASE @param1
    WHEN "1" THEN field1 = something
    WHEN "2" THEN field2 = something
END
Author
1 Jul 2005 4:18 PM
--CELKO--
You do not know what an expression is and that is a more basic
programming issue than just SQL.  Expressions return values; they do
not change the flow of control.  Also, SQL uses single quotes for
strings and double quotes for identifiers.  I am also bothered by the
use of the term "field' in a name; it implies that you do not know that
columns are not fields and still stuck in a procedural code mindset:

Try somethng like this:

SELECT *
  FROM Fooibar
  WHERE
CASE WHEN @param1 = 1 AND field1 = something
          THEN 'T'
          WHEN @param1 = 2 AND field1 = something
          THEN 'T'
           ELSE 'F' END = 'T';



    WHEN "2" THEN field2 = something
END
Author
1 Jul 2005 7:27 PM
Ross Presser
On 1 Jul 2005 09:18:12 -0700, --CELKO-- wrote:

>  I am also bothered by the
> use of the term "field' in a name; it implies that you do not know that
> columns are not fields and still stuck in a procedural code mindset:

Wow, that's awfully mild rebuke by Celko standards. Feeling generous today?
Show quote
:-)
Author
2 Jul 2005 10:08 AM
--CELKO--
>> Feeling generous today?  <<

Tired; I'll do better later.
Author
7 Jul 2005 3:57 PM
Michael C#
It's hard work saving the world, one query at a time :)

Show quote
"Ross Presser" <rpresser@NOSPAMgmail.com.invalid> wrote in message
news:ne6nupyc93qk$.bs35r774fmgw.dlg@40tude.net...
> On 1 Jul 2005 09:18:12 -0700, --CELKO-- wrote:
>
>>  I am also bothered by the
>> use of the term "field' in a name; it implies that you do not know that
>> columns are not fields and still stuck in a procedural code mindset:
>
> Wow, that's awfully mild rebuke by Celko standards. Feeling generous
> today?
> :-)

AddThis Social Bookmark Button