Question : Using IF in SQL SELECT

Hi Experts,
I have a database holding time taken to complete task info; specifically the user enters a deadline date and time and then when a job is started the server time is sent to the database and on completion the same again.

However users wanted the option to amend the original deadline date & /or time but without overwriting the original deadlines in the database. I therefore ended up with 4 columns (DeadlineDate, DeadlineTime, DeadlineAmendDate and DeadlineAmendTime).

So far so good.

They wanted to be able to generate monthly reports showing the number of jobs completed within the deadlines, my question is can I write a SELECT that incorporates an IF to the effect IF DeadlineAmendDate is null use DeadlineDate and ditto that for time, IF DeadlineAmendDate is NOT Null use DeadlineAmendDate.

Can anyone show me how I might incorporate that into my select, so that if the deadline has been amended the new deadline shows instead of the old one but otherwise the old one shows. Or should this be a CASE rather than IF?

Either way be glad of some help.

Answer : Using IF in SQL SELECT

Select case when DeadLineAmendDate is Null then Deadlinedate else DeadLineAmendDate  end as DLDate, case when DeadLineAmendTime is Null then Deadlinetimeelse DeadLineAmendTime end as DLTime from yourtablename

Regards,

Randy

Random Solutions  
 
programming4us programming4us