0

I'm trying to calculate the difference between two hours. However, I would not like to inform the dates. I would like to know for example that between 23:00 to 01:00, 120 minutes have passed.

What I tried to do and got something similar was like this but I had to inform the dates:

DECLARE @antes DATETIME = '2016-10-20 23:00:00.000';

DECLARE @depois DATETIME = '2016-10-21 01:00:00.000';


SELECT  CONVERT(VARCHAR, DATEDIFF(HOUR, @antes, @depois)) + 'H'
        + RIGHT('00' + CONVERT(VARCHAR, DATEDIFF(MINUTE, @antes, @depois) % 60), 2) + ':'
        + RIGHT('00' + CONVERT(VARCHAR, DATEDIFF(SECOND, @antes, @depois) % 60), 2);
Ribak
  • 117
  • 1
  • 3
  • 1
    Well, without a date, this is not strictly possible because of daylight saving time adjustments. For instance, on 28th March here in the UK, the difference between 00:00 and 02:00 was only 60m. – spender May 28 '21 at 16:40
  • 2
    ? .. select (datediff(hour, cast(@antes as time), cast(@depois as time)) + 24) % 24 – lptr May 28 '21 at 16:54
  • Does this answer your question? [SQL time difference between two dates result in hh:mm:ss](https://stackoverflow.com/questions/13577898/sql-time-difference-between-two-dates-result-in-hhmmss) – SMor May 28 '21 at 17:57

0 Answers0