我需要比較2個日期並返回中間的天數。這裏有一個表格爲例:SQL計算Datediff爲8個工作日的一半
+----+--------+-------------------------+-------------------------+
| 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 |
+----+--------+-------------------------+-------------------------+
在前三種情況下,我們有同一天,只有小時不匹配。
Datestarted = 2014-03-25 09:05:00.000
Datefinished = 2014-03-25 12:15:00.000
我們只輸入hours
和minutes
。
直到現在,WEE只需要表現出difference
爲whole number
,沒有decimal points
,並沒有這樣說:
DATEDIFF(carsharing.datestarted, carsharing.datefinished)
但是現在,我們必須顯示的日期之間的difference
爲0,5 day
,如果是less
而不是4,5 hours
。如果差異是greater
它應該保持爲1 day
。
在從表中更complecated最後一種情況下,我們也應該比較並顯示不同的兩天
Datestarted = 2014-03-25 09:05:00.000
Datefinished = 2014-03-26 12:15:00.000
這裏的差異的結果應該是1,5 days
編輯您的樣本數據以表格的形式問題,與期望的結果一起。邏輯很難遵循。 –
您是否考慮在C#中編寫CLR函數以TimeSpan的形式公開您的答案,然後可以將格式設置爲天,小時,分鐘,秒,毫秒的格式? – ewahner
DATEDIFF(datepart,startdate,enddate)https://msdn.microsoft.com/zh-cn/library/ms189794。aspx – PeterRing