Solved

Creating a DB column value by using a formula

Posted on 2008-10-20
3
184 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
3 Comments
 
LVL 22

Expert Comment

by:dportas
Comment Utility
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
Comment Utility
you could also do the same thing without a view by making the TotalTime a derived column.  
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now