我有两个日期:
2012-10-04 12:48:56:000
和2012-10-04 12:48:58:000
预期结果为
2012-10-04 12:48:57:000
2012-10-04 12:48:56:000
和2012-10-04 12:48:56:010
预期结果为
2012-10-04 12:48:56:005
(日期是虚构的,因为在sql server中,毫秒部分DATETIME数据类型增加了3)
答案 0 :(得分:7)
有你自己的约会......
SELECT DATEADD(ms,
DATEDIFF(ms,'2012-10-04 12:48:56:000', '2012-10-04 12:48:58:000')/2,
'2012-10-04 12:48:56:000')
答案 1 :(得分:1)
这样的事情:
with sample_data (start_dt, end_dt) as
(
select cast('2012-10-04 12:48:56:000' as datetime), cast('2012-10-04 12:48:58:000' as datetime)
union all
select cast('2012-10-04 12:48:56:000' as datetime), cast('2012-10-04 12:48:56:010' as datetime)
)
select start_dt, end_dt, dateadd(millisecond, datediff(millisecond, start_dt, end_dt) / 2, start_dt)
from sample_data
虽然第二对计算不正确。可能是因为3毫秒的分辨率。
答案 2 :(得分:1)
declare @date1 datetime;
declare @date2 datetime;
set @date1 = '2012-10-04 12:48:56:000';
set @date2 = '2012-10-04 12:48:58:000';
select DateAdd(ms, DateDiff(ms, @date1, @date2)/2, @date1)
答案 3 :(得分:0)
-- let's day d1 and d2 are DateTime variables (d1 < d2)
-- get the differnce in milliseconds
-- (you can change it but be careful with oveflow situations)
declare @diff integer = datediff (ms, @d1, @d2)
-- the middle is the first date + half of the difference
declare @middle DateTime = dateadd (ms, @diff / 2, @d1)
答案 4 :(得分:0)
我需要能够通过MySQL和MariaDB做到这一点。这是有效的:
SELECT DATE_ADD('2012-10-06 12:48:58.000', INTERVAL time_to_sec(TIMEDIFF('2012-10-04 12:48:56.000', '2012-10-06 12:48:58.000'))/2 SECOND)
结果:2012-10-05 12:48:57.000000
答案 5 :(得分:-1)
试试这个(你可以根据你想要的准确度来替换日期部分):
DateAdd(ms, DateDiff(ms, date1, date2), date1)/2