Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my web application i have to insert the time (hr:min:sec format) taken to complete a sum to database. If a person doing the same problem for the second time then our time must add. That means if the time taken to complete a sum for the first time is 01:10:55 and if the same sum is doing again with a time of 01:25:10, then i have to add both the time (the time ll b 02:36:05).i have to insert this time to database. What type of datatype should i use.

help me to do this in sql server 2008 stored procedure....
Posted
Updated 10-Mar-13 20:25pm
v3

Hi,

probably, the datatype of your table column is smalldatetime. change that to "DateTime". this will provide accuracy up to milliseconds. And insert the value using "GETDATE()" function, which will give complete date with time. Ex. 2013-02-19 10:44:36.410, with seconds and milliseconds.
SQL
INSERT INTO TableName (ColumnName) VALUE(GETDATE())


hope it helps
 
Share this answer
 
v3
Hi,
From your description above this could be a solution.


SQL
DECLARE @T TABLE (AttemptDiff TIME)

DECLARE @FirstTry        DATETIME,
        @SecondTry       DATETIME,
        @DiffSeconds     INT

SET @FirstTry = GETDATE()

WAITFOR DELAY '00:00:05' --Assume user attempt for second try  after 5 seconds

SET @SecondTry = GETDATE() --grab the second try

SET @DiffSeconds = DATEDIFF(SECOND, @FirstTry, @SecondTry) --get the difftime

--SELECT @DiffSeconds

--SELECT @FirstTry

SET @FirstTry = DATEADD(second, @DiffSeconds, @FirstTry) --Add it to the first try

INSERT INTO @T
SELECT CONVERT(VARCHAR, @FirstTry, 108) -- Insert it to table.


SELECT * FROM @T t


This solution could work fro SQL Server 2012 for other version keep the time as varchar.

Enjoy!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900