如何在所有条件下查找时差
如果仅相差几分钟
49 mintus
如果小时数不同
1 hour 20 minutes
我正在尝试
SELECT DATEDIFF(MINUTE,'2018-08-09 10:16:49.000','2018-08-09 11:14:40.000') AS Diff
但这仅在几分钟内显示
我也尝试过这个
DECLARE @StartTime datetime = '2018-08-09 10:16:49.000',
@EndTime datetime = '2018-08-09 12:44:05.000'
SELECT CAST(@EndTime - @StartTime as Time) As TimeDiffere
输出:02:27:16.0000000
但是我想2hr 27mins 16Sec
答案 0 :(得分:3)
如果您的SQL Server版本高于2012,则可以尝试起诉format函数。
CREATE TABLE T(
StartTime datetime,
EndTime datetime
);
insert into t values ('2018-08-09 10:16:49.000','2018-08-09 12:44:05.000')
查询1 :
SELECT format(EndTime - StartTime ,'hh\hr mm\min\s ss\Sec') As TimeDiffere
FROM T
Results :
| TimeDiffere |
|-------------------|
| 02hr 27mins 16Sec |
注意
反斜杠\
可以像format
,hr
,min
...这样从s
逃逸关键字,然后将其显示为原始单词。 / p>
答案 1 :(得分:1)
DECLARE @StartTime datetime = '2018-08-09 10:01:15.000',
@EndTime datetime = '2018-08-09 12:50:00.000'
SELECT CONCAT((DATEDIFF(second,@StartTime,@EndTime )/60)/60,'hr ',(DATEDIFF(second,@StartTime,@EndTime)/60)%60,'mins ',DATEDIFF(second,@StartTime,@EndTime)%60,'Sec')
As TimeDiffere
答案 2 :(得分:0)
original_pic = Photo.objects.filter(id=photo_id).values_list('original_pic', flat=True)[0]
image = Image.open(original_pic).convert('RGBA')
答案 3 :(得分:0)
如果您的datetime
功能差,无法获得hh.mm.ss
,则可以使用apply
:
select t.*, concat( (dtdiff / 3600), 'hr ', (dtdiff / 60) % 60, 'mins ', dtdiff % 60, 'sec')
from table t cross apply
( values (datediff(second, startdate, enddate))
) tt(dtdiff);