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.
| Author |
Topic |
|
versatilewt
Starting Member
2 Posts |
Posted - 2006-04-20 : 09:20:24
|
Hi, I'm writing some complex queries on a very poorly designed database. We're running on MS SQL Server2000. I was wondering if its possible to do in-line if-else blocks within a query.. i.e.SELECT * FROM tablename IF (condition1 = 'a') WHERE condition2 = 'a' ELSE WHERE condition2 = 'b' Thanks, Brian |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-04-20 : 09:25:07
|
Use a CASE statement. i.e. WHERE condition2 = CASE WHEN condition1 = 'a' THEN 'a' ELSE 'b' END Mark |
 |
|
|
versatilewt
Starting Member
2 Posts |
Posted - 2006-04-20 : 11:29:36
|
Mark, Thanks for the reply. I think I over simplified my question a bit. Here's exactly what I'm trying to do. I want to set WHERE conditions optionally, based on a subquery. The data isn't given explicity, so I have to kind of infer it through the query. I want to set a where condition of e.stafflevelid = m.stafflevelid and project_dept = manager_dept ONLY when then Manager level is, say 4 (which is determined from a subquery).So, for the queryWHERE e.active <> 0 AND /* THIS IS THE OPTIONAL WHERE CONDITION */ /* IF THE MANAGER'S STAFF LEVEL SATISFIES SUBQUERY*/ (mgr.stafflevelid IN (/* HERE WOULD BE SUBQUERY) /* THEN SET THESE WHERE CONDITIONS*/ e.stafflevelid=m.stafflevelid p.dept = m.dept) And, I want this condition to run with other WHERE statementsI know it's not pretty, but I'm working with bad structure.Thanks, Brian |
 |
|
|
|
|
|