0

我需要以下列方式在表中包含两列:

OverallTime | ElapsedTime

10          | 3:12:30

所以 10 是天数,3:12:30 是 3 天 12 小时 30 分钟。然后我需要计算TimeLeft哪个是

OverallTime - ElapsedTime

什么是可以在 sql DB 中表示这个的 correcy 数据类型?

这适用于 SQL Server 2008 R2

4

1 回答 1

1

对于 Sql-Server:OverallTime = daysElapsedTime = 'days:hours:minutes'

declare @d datetime= getdate()

select OverallTime, 
       OverallTime + ':' + convert(varchar(5), @d, 108) ElapsedTime
from (
  select right(convert(varchar(8), @d, 112),2) OverallTime
  ) x

演示 1

| OVERALLTIME | ELAPSEDTIME |
-----------------------------
|          07 |    07:17:32 |

如果您的意思是您的日期如图所示并且需要查找,请OverallTime - ElapsedTime尝试(替换@ot@et列名);

declare @ot int = 10, @et varchar(10) = '3:12:30'

select datediff(minute,time, OverallTime-days) [OverallTime - ElapsedTime]
from (
select  @ot OverallTime, convert(int,left(@et,charindex(':',@et,1)-1)) days,
             right(@et, len(@et) - charindex(':',@et,1)) + ':00' time
  ) x

演示 2

| OVERALLTIME - ELAPSEDTIME |
-----------------------------
|                      9330 |
于 2013-03-07T17:32:45.863 回答