如何在hr:min中添加时间:sql查询中的秒格式 [英] How To add time in hr:min:Sec format in sql query

查看:77
本文介绍了如何在hr:min中添加时间:sql查询中的秒格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我必须插入时间(hr:min:sec格式)以完成数据库的总和。如果一个人第二次做同样的问题,那么我们的时间必须加上。这意味着如果第一次完成总和所花费的时间是01:10:55并且如果相同的总和再次以01:25:10的时间进行,那么我必须添加两个时间(时间ll b 02:36:05)。我必须将此时间插入数据库。我应该使用什么类型的数据类型。



帮助我在sql server 2008存储过程中执行此操作....

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....

推荐答案





可能,表列的数据类型是smalldatetime。将其更改为DateTime。这将提供高达毫秒的准确度。并使用GETDATE()函数插入值,该函数将提供完整的日期和时间。 实施例。 2013-02-19 10:44:36.410 ,秒和毫秒。

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


您好,

从您上面的描述中可以找到解决方案。





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





这个解决方案可以起作用于SQL Server 2012 for other version keep t ime as varchar。



享受!!!



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

Enjoy!!!


这篇关于如何在hr:min中添加时间:sql查询中的秒格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆