0

再会。我使用 ms sql server 2005 express 并继承了我的表结构:

      User  |        Log_Date         |     Log_In_Time         |       Log_Out_Time      |
-----------------------------------------------------------------------------------------
    xxx     | 2013-10-02 23:00:31.623 | 2013-10-02 23:07:31.623 | 2013-10-03 01:08:31.623 |

笔记:

LOG_OUT_TIME是在其他日子,即10 月 3 日上午 1:08LOG_IN_TIME10 月 2 日晚上 11:00

现在我想添加列: TOTAL

它看起来像这样:

      User  |        Log_Date         |     Log_In_Time         |       Log_Out_Time      | TOTAL  |
---------------------------------------------------------------------------------------------------
    xxx     | 2013-10-02 23:00:31.623 | 2013-10-02 23:07:31.623 | 2013-10-03 01:08:31.623 | 2.08   |

有人能知道路吗?

先感谢您!

4

4 回答 4

2

很简单的方法:

CAST(Log_Out_Time - Log_In_Time AS TIME)

给出:

02:01:00.0000000

编辑:

准确查询:

DECLARE @ST DATETIME = '2013-10-02 23:07:31.623'
DECLARE @ET DATETIME = '2013-10-03 01:08:31.623'

SELECT CAST(@ET - @ST AS TIME)
于 2013-10-23T09:57:33.883 回答
1

这将起作用:

SELECT CAST(DATEDIFF(hour, Log_In_Time, Log_Out_Time) AS varchar) + '.' + 
    CAST(DATEDIFF(mi, Log_In_Time, Log_Out_Time) / (60 * DATEDIFF(hour, Log_In_Time, Log_Out_Time) ) AS varchar)
FROM tableName

我已经分别计算了小时和分钟。

于 2013-10-23T09:41:18.003 回答
0

You could use DateDiff @Unknownymous, what I would do is seconds comparison, cast this to a float and the divide by 60 as necessary. This will give you minutes, hours, etc scaling up to the precision you need.

DECLARE @diff float
DECLARE @diffMin float
DECLARE @diffHour float
DECLARE @diffDay float

SELECT @diff = cast(datediff(ss,Log_in_Time,Log_Out_Time) as FLOAT)
SELECT @diffMin = @diff/60
SELECT @diffHour = @diffMin/60
SELECT @diffDay = @diffHour/24

print @diff
print @diffmin
print @diffhour
print @diffday
于 2013-10-23T09:40:32.663 回答
0
select 
cast(datepart(hour,Log_Out_Time - Log_Date) as varchar) + '.' +  
right('00' + cast(datepart(minute,Log_Out_Time - Log_Date) as varchar),2)
于 2013-10-23T09:48:19.620 回答