我需要比较2个日期并返回两者之间的天数。这是一个表格示例:
+----+--------+-------------------------+-------------------------+
| id | userid | datestarted | datefinished |
+----+--------+-------------------------+-------------------------+
| | | | |
| 1 | 23 | 2014-03-25 09:05:00.000 | 2014-03-25 12:15:00.000 |
| 2 | 43 | 2014-03-25 09:05:00.000 | 2014-03-25 12:15:00.000 |
| 3 | 23 | 2014-03-31 09:05:00.000 | 2014-03-31 12:15:00.000 |
| 4 | 12 | 2014-03-25 09:05:00.000 | 2014-03-26 12:15:00.000 |
+----+--------+-------------------------+-------------------------+
在前3个案例中,我们在同一天,只有几个小时不匹配。
Datestarted = 2014-03-25 09:05:00.000
Datefinished = 2014-03-25 12:15:00.000
我们只输入hours
和minutes
。
到目前为止,我们只需将difference
显示为whole number
,而不是decimal points
,并且这样做:
DATEDIFF(carsharing.datestarted, carsharing.datefinished)
但是现在,我们必须在difference
之间显示0,5 day
,如果它是less
而不是4,5 hours
。如果差异为greater
,则应保持为1 day
。
在表中更复杂的最后一个案例中,我们还应该比较并显示两个不同日期之间的差异
Datestarted = 2014-03-25 09:05:00.000
Datefinished = 2014-03-26 12:15:00.000
此处的结果应为1,5 days
答案 0 :(得分:2)
我相信这是你正在寻找的东西 - 这将把当天4.5小时以内的任何东西的差异缩小到0.5,而其他所有东西都会花一整天:
Declare @StartDate DateTime = '2014-03-25 09:05:00.000',
@EndDate DateTime = '2014-03-26 12:15:00.000'
;With TotalHours As
(
Select DateDiff(Minute, @StartDate, @EndDate) / 60.0 As TotalHours
)
Select Case
When TotalHours % 24 = 0
Then Floor(TotalHours / 24)
When TotalHours % 24 < 4.5
Then Floor(TotalHours / 24) + 0.5
Else Floor(TotalHours / 24) + 1.0
End As Days
From TotalHours
答案 1 :(得分:1)
DECLARE
@StartDate datetime = '2014-03-25 09:05:00.000'
,@EndDate datetime = '2014-03-26 09:05:00.000'
;WITH d AS (SELECT DATEDIFF(d,@StartDate,@EndDate) Dys)
,h AS (SELECT DATEDIFF(hh,@StartDate,@EndDate) Hrs)
SELECT d.Dys + CASE WHEN (h.Hrs - d.Dys*24) = 0 THEN 0 ELSE CASE WHEN (h.Hrs - d.Dys*24) < 4.5 THEN 0.5 ELSE 1 END END
FROM d,h
答案 2 :(得分:1)
您可以尝试此查询。它以分钟为单位得到差异并乘以2以获得0.5天的范围。然后在计算天花板值之前将其分为24小时60分钟。一旦你拥有它,它可以再次偏离2。
当价值超过4.5 * 24 * 60(4.5天(分钟))时,只需要分成24和60.
<强>查询:强>
Select id, userid, datestarted, datefinished
, Days = Case When DATEDIFF(minute, datestarted, datefinished) > 4.5*60*24
then DATEDIFF(minute, datestarted, datefinished) / 24 / 60
else CEILING(((2.0*DATEDIFF(minute, datestarted, datefinished)) / 24 / 60)) / 2
end
From @dates
<强>输出:强>
id userid datestarted datefinished Days
1 23 2014-03-25 09:05:00.000 2014-03-25 12:15:00.000 0.500000
2 43 2014-03-25 09:05:00.000 2014-03-25 12:15:00.000 0.500000
3 23 2014-03-31 09:05:00.000 2014-03-31 12:15:00.000 0.500000
4 12 2014-03-25 09:05:00.000 2014-03-26 12:15:00.000 1.500000
5 12 2014-03-25 09:05:00.000 2014-03-29 12:15:00.000 4.500000
6 12 2014-03-25 09:05:00.000 2014-03-29 22:15:00.000 4.000000
示例数据
declare @dates table(id int, userid int, datestarted datetime, datefinished datetime);
insert into @dates(id, userid, datestarted, datefinished) values
(1, 23, '2014-03-25 09:05:00.000', '2014-03-25 12:15:00.000')
, (2, 43, '2014-03-25 09:05:00.000', '2014-03-25 12:15:00.000')
, (3, 23, '2014-03-31 09:05:00.000', '2014-03-31 12:15:00.000')
, (4, 12, '2014-03-25 09:05:00.000', '2014-03-26 12:15:00.000')
, (5, 12, '2014-03-25 09:05:00.000', '2014-03-29 12:15:00.000')
, (6, 12, '2014-03-25 09:05:00.000', '2014-03-29 22:15:00.000')