Home > Software engineering >  Is timestamp difference always INTERVAL DAY TO SECOND?
Is timestamp difference always INTERVAL DAY TO SECOND?

Time:01-06

My observations show that timestamp difference has type of INTERVAL DAY TO SECONDS:

select typeof(getdate() - current_date())
-----------------------------------------
    
interval day to second

But is it guaranteed? Can it be DAY TO MINUTE or YEAR TO MONTH depending on the input? I could not find that in the documentation.

This becomes important when converting an interval to numeric format, since CAST() returns the number of lower units in the interval.

P.S. I am aware of other ways to convert time delta into seconds.

CodePudding user response:

It is documented:

If expr1 and expr2 are DATEs the result is an INTERVAL DAYS.

If expr1 or expr2 are TIMESTAMP the result is an INTERVAL DAY TO SECOND.

It can be INTERVAL YEAR TO MONTH only when subtracting two year-month intervals.

And I think you can't cast an interval to a number...

  • Related