Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Days Unavailable

I need a simple SQL to accomplish the below:

Problem:

When a petrol bunk runs out of fuel, the admin makes note of the DateTime (RunOutDate) when it ran out of fuel and notes also the DateTime (ResupplyDate) when the fuel supply was back on.

I need to create a report on how many days the bunk ran out of fuel.

eg.

1/1/1 10:10 to 1/1/1 10:50 should be counted as 1

1/1/1 10:10 to 2/1/1 07:20 should be counted as 2

1/1/1 23:55 to 2/1/1 00:10 should be counted as 2

I can not bank using hours using DateDiff as 24 hours could have spanned across 2 days.

TIA

like image 611
reva Avatar asked Feb 05 '26 09:02

reva


1 Answers

DATEDIFF(d, RunOutDate, ResupplyDate) + 1

Remember that DATEDIFF always counts the number of BOUNDARIES that you cross. For days (first argument d), it counts the number of times the clock passed midnight. So to count the number of days covered you just add 1.

like image 59
Joel Spolsky Avatar answered Feb 08 '26 06:02

Joel Spolsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!