Re: Rewrite a WHERE clause
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 21:22:20 +0000 (UTC)
joshsackett (joshsackett@xxxxxxxxx) writes:
> Morgan:
> I don't understand quite what you mean. Can you use my query above and
> fit it to your example?
What he said is that:
WHERE (CASE WHEN @COUNTY IS NOT NULL COUNTY = @COUNTY ELSE COUNTY LIKE
'%' END)
does not work, because you have mixed the CASE expression with the WHERE
condition. You could say:
WHERE COUNTY = CASE WHEN @COUNTy IS NOT NULL THEN @COUNTY ELSE COUNTY END
or briefer:
WHERE COUNTY = coalesce(@COUNTY, COUNTY)
(coalesce is just syntactic sugar for CASE WHEN x IS NOT NULL THEN x WHEN
....)
Or, as said earlier in the thread you could go for some elaborate trick for
dynamic searches.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
.
- References:
- Rewrite a WHERE clause
- From: joshsackett
- Re: Rewrite a WHERE clause
- From: --CELKO--
- Re: Rewrite a WHERE clause
- From: Erland Sommarskog
- Re: Rewrite a WHERE clause
- From: --CELKO--
- Re: Rewrite a WHERE clause
- From: joshsackett
- Rewrite a WHERE clause
- Prev by Date: Re: Rewrite a WHERE clause
- Next by Date: Re: Strange date/time anomaly, or am I just stoopid?
- Previous by thread: Re: Rewrite a WHERE clause
- Next by thread: Re: Rewrite a WHERE clause
- Index(es):
Relevant Pages
|