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 |
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-23 : 12:22:30
|
Hi,I am trying to get my SP to work with the following@StartDate Varchar(20),@EndDate Varchar(20)Select * from mytable where mydate => @Startdate and myDate <= @EndDate The above is fine, however I need to insert a condition in between this statement as follows:if the field mytable.status = 'CLOSED' then I want to substitute mydate with mydateClosed.basically, depending on the value of Mytable.status I need to change the SELECT statement from mydate to mydate1is there an easy way to do this?thanksCraig. |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2009-03-23 : 12:44:42
|
yes.. you can use CASE in your WHERE clause to accomplish this..Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-23 : 12:51:04
|
Hi,I dont think that will work, the "Select" statement has to be dymanic based on the value of mytable.status.ie. if status = closed thenselect * from mytable where mydate between @startdate and @enddateelseselect * from mytable where MYDATE1 between @startdate and @enddate |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-23 : 12:52:06
|
[code]SELECT * FROM(Select *,case when status = 'CLOSED' then mydateClosed else mydate end as DateVal from mytable )twhere DateVal => @Startdate and DateVal <= @EndDate[/code] |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-24 : 07:10:38
|
Thanks , I've got it working now! |
|
|
|
|
|