all groups > sql server (alternate) > may 2004 >
You're in the

sql server (alternate)

group:

CASE with empty parameter



CASE with empty parameter Not Me
5/28/2004 11:30:51 AM
sql server (alternate): Hi, I can't seem to get this where clause to function as I wish.. must be
something simple but I can't see it!

have tried
ac.coursename like CASE @course WHEN null THEN '%' ELSE '%' + @course + '%'
ac.coursename like CASE @course WHEN '' THEN '%' ELSE '%' + @course + '%'
ac.coursename like CASE len(@course) WHEN 0 THEN '%' ELSE '%' + @course +
'%'

All work fine when I enter something in the parameter, but not when I leave
it blank! Any ideas why this is the ...case?

Cheers!
Chris

Re: CASE with empty parameter Not Me
5/28/2004 11:52:47 AM
[quoted text, click to view]

Ok try #4, I did
ac.coursename like CASE WHEN @course is null THEN '%' ELSE '%' + @course +
'%'

And it works! Don't you just hate discovering the answer yourself straight
after asking the question? :op

Chris

Re: CASE with empty parameter Not Me
5/28/2004 1:43:48 PM
[quoted text, click to view]

New problem :)

Along the same lines as above, i.e. wanting to include all rows if no
criteria specified - What can I do in the case of integers? what would be
the %/catch-all for this?
Is there a better way alltogether of using 'optional' parameters?

Any help much appreciated!
Chris



Re: CASE with empty parameter tperovic
5/28/2004 1:44:53 PM
I've been using this:

ac.coursename = COALESCE(@course,ac.coursename)

When @course is null then the column is compared to itself which is always
true.


[quoted text, click to view]

Re: CASE with empty parameter Not Me
5/28/2004 3:20:30 PM
[quoted text, click to view]

Aha, so much better! Thanks for that.

Chris

Re: CASE with empty parameter Erland Sommarskog
5/29/2004 10:17:00 PM
Not Me (Not.Me@faker.fake.fa.kee) writes:
[quoted text, click to view]

Depends on your business domain, but NULL would certainly be the
best choice.

[quoted text, click to view]

Don't really know what you are into, but
http://www.sommarskog.se/dyn-search.html might be something for you.


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
Re: CASE with empty parameter Not Me
6/1/2004 9:34:15 AM

[quoted text, click to view]

Thanks for that!

Chris

AddThis Social Bookmark Button