Skip to content
Advertisement

what’s the difference between DATEDIFF and doing subtraction by myself

the leetcode question 197.Rising Temperature


Given a Weather table, write a SQL query to find all dates’ Ids with higher temperature compared to its previous (yesterday’s) dates.


i didn’t know the function DATEDIFF(),so i wrote my sql solution:

and i went through the testcase but got a wrong submit,the right solution is use funtion DATEDIFF()

so my question is what’s the difference between DATEDIFF(w1.RecordDate,w2.RecordDate)=1and w1.RecordDate - w2.RecordDate = 1

thank you for your help

Advertisement

Answer

If the datatype of RecordDate is DATETIME rather than DATE, subtracting them returns a large value that contains the difference between the times as well as the dates. E.g.

But if they’re DATE then subtraction should be the same as DATEDIFF():

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement