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 |
|
MakWebster
Starting Member
3 Posts |
Posted - 2007-06-16 : 00:30:09
|
| Hi,I am trying to write this rather simple query and cant get it to work:DECLARE @T INT SET @R = T -- 0|1|2SELECT * FROM TEST WHERE TestID in (1,2)AND CASE WHEN @R = 1 THEN TestDate IS NULL ELSE TestDate is NOT NULL ENDBasically what i am trying to achieve is that @T can be assigned only 0,1 or 2 and based on this i have to select TestDate column which can have either NULL or NOT NULL value.so i can say if @t is 0 show all, when 1 show not nulls when 2 show nulls.Any clever way of doing this?Help is much appreciatedMak |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-16 : 00:38:39
|
Simple approachDECLARE @T INT SET @R = T -- 0|1|2IF @R = 1 SELECT * FROM TEST WHERE TestID in (1,2) AND TestDate IS NULL ELSE SELECT * FROM TEST WHERE TestID in (1,2) AND TestDate IS NOT NULL MadhivananFailing to plan is Planning to fail |
 |
|
|
MakWebster
Starting Member
3 Posts |
Posted - 2007-06-16 : 06:03:27
|
| Thanks for the reply, the stored procedure has got several select statements each one of them has different WHERE clause and this check is generic that need to go with all the statements.thats why i like to do inline check witin the WHERE clausere-writing proc is not really an optionAny more ideas ? |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-06-16 : 08:38:36
|
| [code]DECLARE @T INTSET @R = @T -- 0|1|2SELECT * FROM TEST WHERE TestID in (1,2)AND( ( @R = 1 And TestDate IS NULL ) OR ( @R = 2 And TestDate is NOT NULL ) )[/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
MakWebster
Starting Member
3 Posts |
Posted - 2007-06-18 : 10:00:17
|
| Thanks to all particularly to chiragkhabaria, this is exactly what i wanted |
 |
|
|
|
|
|
|
|