Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C ASP SQL .NET
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 18-Feb-13 19:10pm
shamjid1.5K
Edited 10-Mar-13 21:25pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
INSERT INTO TableName (ColumnName) VALUE(GETDATE())
 
hope it helps
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
From your description above this could be a solution.
 

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!!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100