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
 IF / When in a select statement

Author  Topic 

robtop
Starting Member

9 Posts

Posted - 2011-07-28 : 07:46:14
Afternoon,

I've been scratching my head for a while now trying to figure out how to replicate a formula i have in excel into a sql select statement.

What i want to do is:

IF END_DATE is null Then show the no of days between TO_DATE & FROM_DATE, IF END DATE is not null then show the number of days between END_DATE & FROM date.

Any assistanc on how to include this in a select statement would be much appreciate. Also would liek to knwo if you can put this into the design grid & if so where.

Thanks very much and if you need any more info please let me know

Rob

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-28 : 08:12:07
Something like this?
SELECT 
CASE
WHEN end_date IS NULL THEN DATEDIFF(DAY, from_date, to_date)
ELSE DATEDIFF(DAY, end_date, from_date)
END
FROM
yourTable
Go to Top of Page

robtop
Starting Member

9 Posts

Posted - 2011-07-28 : 08:24:18
Thanks very much that works perfectly and is a great help
Go to Top of Page
   

- Advertisement -