我們有要求每天向我們的客戶開票的要求。我們在我們的系統當天的賬單中存在資產存在。於是,我開始用DATEDIFF ...T-SQL計算兩天之間的天數(datediff不太適用)
select datediff(dd ,'2015-04-24 12:59:32.050' ,'2015-05-01 00:59:59.000');
返回此:
7
但我需要算日期如下:4/24,4/25,4/26,4/27 ,4/28,4/29,4/30,5/1,這8天。所以datediff不是很有效。我想這些變化下面
--too simple, returns 7, i need it to return 8
select datediff(dd ,'2015-04-24 12:59:32.050', '2015-05-01 23:59:59.000');
--looking better, this returns the 8 i need
select ceiling(datediff(hh,'2015-04-24 12:59:32.050', '2015-05-01 23:59:59.000')/24.0);
-- returns 7, even though the answer still needs to be 8. (changed enddate)
select ceiling(datediff(hh,'2015-04-24 12:59:32.050', '2015-05-01 00:59:59.000')/24.0);
所以,我的問題...如何在SQL,我會得到的日期算像我所描述的,因爲我相信DATEDIFF計數跨越時間界限的數量....我當前最好的方法是循環遍歷遊標和計數。伊克。
select datediff(dd,'2015-04-24 00:00:00.000','2015-05-01 23:59:59.999'); – Lali
您使用datediff計算邊界的數量是正確的。如果你想包含開始日期,你不能只加1結果? 'select datediff(dd,'2015-04-24 12:59:32.050','2015-05-01 23:59:59.000')+ 1;'? – jpw