computed column SQL 2008 if statement

I have a duration column in SQL that is (datediff(minute,[InTime],[OutTime])). Then I sum the status during that duration to get the % time spent in each status. My question is the most current status does not have an OutTime yet and therefore no duration, thus it is being excluded from the %.

What I would like to do is use Getdate() as the OutTime so that when a query is ran it is always recalculated to the current time. Key is that OutTime needs to remain null.

I was thinking (if(OutTime is null, datediff(minute,[InTime],GetDate()), datediff(minute,[InTime],[OutTime])))

I get an error validating field when I do that. I guess I could throw it in a view and query that instead.
bhiebAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Use this:
 (datediff(minute,[InTime],IsNull([OutTime],GetDate())))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhiebAuthor Commented:
Perfect, so as long as I don't make this persistent it will recalc every time it is called?
0
Walter RitzelSenior Software EngineerCommented:
Yes.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
<Wild Guess>  
Same expression you have now, but use an ISNULL function to replace null values with the GETDATE:

Declare @dt datetime = GETDATE()
SELECT ... 
datediff(minute,[InTime],ISNULL([OutTime], @dt)) as duration

Open in new window


A case can also be made to remove all rows WHERE OutTime IS NULL from the query, as it is somewhat of a false statement as who knows how long the duration will be of what's in progress..
0
JestersGrindCommented:
You should be able to use ISNULL.  

DATEDIFF(MINUTE, [InTime], ISNULL([OutTime], GETDATE()))

Open in new window


Hope this helps,

Greg
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.