菜鸟求助!sql数据库同一个表中,结束时间减开始时间后赋值给时间长度列的问题
各位大虾:小弟想在sql数据库中编写一条“EndTime - StartTime” 值赋给新列“TimeLong”效果如:
StartTime EndTime TimeLong
2007-8-1 12:30:25 2007-8-1 13:25:30 00:50:05
该怎么写具体SQL语句?谢谢!!!!!!!!!!!!!
[解决办法]
用datediff(ss,StartTime,EndTime)的到两个时间相差的秒数
Create Table TimeDiff
(
Code Char(3)
,BeginTime DateTime
,EndTime DateTime
,TimeLong DateTime
)
Insert TimeDiff
Select 'A ',GetDate(),Dateadd(ss,400,getdate()), ' '
union all
Select 'B ',GetDate(),Dateadd(ss,7000,getdate()), ' '
Select * From TimeDiff
Update TimeDiff Set TimeLong =Convert(VarChar,DateDiff(ss,BeginTime,EndTime)/3600)+ ': '+Convert(VarChar,DateDiff(ss,BeginTime,EndTime)%3600/60)+ ': '+Convert(VarChar,DateDiff(ss,BeginTime,EndTime)%60)
Select Right(Convert(VarChar,TimeLong,120),8) As TimeLong From TimeDiff
/*
TimeLong
----------------
00:06:40
01:56:40
(2 行受影响)
*/