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
 General SQL Server Forums
 New to SQL Server Programming
 Inserting conditions in a select statement

Author  Topic 

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-12 : 21:17:15
How can i insert a if-else condition in a select statement or is there a way to specify a condition within a select statement?
Any reply is really appreciated..thank you..

shemay

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-12 : 21:21:41
Have details? Checked case clause?
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-12 : 21:47:42
I already tried the case clause but it won't work in my script.The scenario is this if the column DateofTransfer is > EndDate, DateofTransfer should be equal EndDate,else DateofTransfer remains the same.

shemay
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-12 : 22:41:43
What was your statement with case?
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-12 : 22:45:02
SELECT a.m0020_pat_id, a.M0040_PAT_LNAME + ', ' + a.M0040_PAT_FNAME + ' ' + a.M0040_PAT_MI as patientName,
a.M0030_START_CARE_DT as socdate,M0906_DC_TRAN_DTH_DT as transferdate,M0906_DC_TRAN_DTH_DT+14 as DueDate = Case when M0906_DC_TRAN_DTH_DT+14 > a.EffectiveDate_End then M0906_DC_TRAN_DTH_DT+14 = a.EffectiveDate_End else M0906_DC_TRAN_DTH_DT+14 end
d.name facilityName, e.FacilityTypeName


Funnyfrog
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-12 : 23:23:21
Tried this?

SELECT a.m0020_pat_id, a.M0040_PAT_LNAME + ', ' + a.M0040_PAT_FNAME + ' ' + a.M0040_PAT_MI as patientName,
a.M0030_START_CARE_DT as socdate,M0906_DC_TRAN_DTH_DT as transferdate,DueDate = Case M0906_DC_TRAN_DTH_DT+14 when M0906_DC_TRAN_DTH_DT+14 > a.EffectiveDate_End then M0906_DC_TRAN_DTH_DT+14 = a.EffectiveDate_End else M0906_DC_TRAN_DTH_DT+14 end
d.name facilityName, e.FacilityTypeName
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-12 : 23:32:15
Do i neeed to set parameters before using the due date column in the case when statement?

Funnyfrog
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-12 : 23:42:16
if i will place the case when statement in the where clause,would that still be applicable?

Funnyfrog
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-12 : 23:57:50
Use subquery for search condition?
Go to Top of Page

shemayb
Posting Yak Master

159 Posts

Posted - 2007-06-13 : 00:09:29
would it be alright to do this?

WHERE DueDate = Case M0906_DC_TRAN_DTH_DT+14 when DueDate > EffectiveDate_End then M0906_DC_TRAN_DTH_DT+14 = EffectiveDate_End else M0906_DC_TRAN_DTH_DT+14 end


Funnyfrog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-13 : 08:37:56
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=84942

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-13 : 10:50:00
Not sure what you want, where clause is for search condition but your code doesn't look like search criteria.
Go to Top of Page
   

- Advertisement -