Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 'Case' within 'Where'

Author  Topic 

augustin_p
Starting Member

21 Posts

Posted - 2002-05-30 : 06:45:29
Hi,
Is it possible to use Case Statements within 'Where' Clause?

Thanks,
prasanna

MakeYourDaddyProud

184 Posts

Posted - 2002-05-30 : 06:54:10

declare @u int
select * from sysobjects
where name like (CASE WHEN @u = 1
THEN '%fil%'
ELSE '%sys%'
END)

Unfortunately, some compromise over query performance may result. Please be sure to look at the QEP for your specific implementations. You may find table scans occur dep on your use of it.

Go to Top of Page

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2002-05-30 : 08:12:40
You should distinguish between case statements and case expressions. danny2sweet uses a case expression, not a case statement. There is a case statement in SQL which is not supported by T-SQL. A case statement is a control statement and cannot be used in a where clause. The syntax is

case expression
when expression_1 then
statement; ...
when expression_2 then
statement; ...
else
statement; ...
end case;

or

case
when expression_1 then
statement; ...
when expression_2 then
statement; ...
else
statement; ...
end case;

Go to Top of Page
   

- Advertisement -