SELECT DISTINCT ORDERNO,MAX(DATEPART(DAY,LoadedStartDate))-MIN(DATEPART(DAY,LoadedStartDate)) FROM ORDERS GROUP BY ORDERNO
Remember, you can use:
SELECT DISTINCT ORDERNO,MAX(LoadedStartDate)-MIN(LoadedStartDate) FROM ORDERS GROUP BY ORDERNO
which returns 4 days in date and time format; possibly because without any time and because of the nature of MIN and MAX, the database assumes four whole days from the beginning of the first day to the end of day 4.
If you have time to order, and you want to get the exact time interval between the first and last order, it may be possible to convert both datetime times to an integer, which each gives in milliseconds, subtract the smaller number from the higher number, and then repeatedly translate the response back to the date and time format.
source share