Solved

Creating a DB column value by using a formula

Posted on 2008-10-20
3
189 Views
Last Modified: 2010-03-20
Hello Experts,

I have a database table with a Startdate and Endtime column and Total Time Column. I would like to DBMS to automatically calculate my Total Time column. Basically I want to see how long it took a process to finish and have the value in Total Time column. How would I go about achieving this. The Startdate and Enddate columns are datetime.
0
Comment
Question by:rhender9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 22760433
Drop the TotalTime column from your table and put it in a view instead.

CREATE VIEW v1 AS
SELECT StartDate, EndDate,
 DATEDIFF(MINUTE, StartDate, EndDate) TotalTime
FROM tbl ;
0
 
LVL 4

Expert Comment

by:randy_knight
ID: 22760682
you could also do the same thing without a view by making the TotalTime a derived column.  
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 22761607
I suggest a computed column.  You define this once to SQL and you can reference from then on as if it were a "real" column.  

ALTER TABLE tablename
    ADD TotalTime AS RIGHT(' ' + CAST(DATEDIFF(MINUTE, Startdate, Endtime) / 60 AS VARCHAR(2)), 2) + ':' + RIGHT('0' + CAST(DATEDIFF(MINUTE, Startdate, Endtime) % 60 AS VARCHAR(2)), 2)


Then:

SELECT Startdate, Endtime, TotalTime, ...
FROM ...
WHERE TotalTime >= ' 1:00'  --everything >= 1 hour (for example)
ORDER BY TotalTime
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question