Home > database >  What is the intent of this SQL statement?
What is the intent of this SQL statement?

Time:10-12

I ran into the following:

DECLARE @UpdateDtm datetime = DATEADD(dd, 0, DATEDIFF(dd, 0, DATEADD(minute, -10, GETDATE())))
SELECT @UpdateDtm

It returns today's date: 2021-10-11 00:00:00.000, but I am not exactly understanding the logic. First it deducts 10 minutes from GetDate, then it gets the difference in days between that and beginning of time. Then it adds that difference (in days, once again) to the beginning of time.

From what I can tell, it could simply be replaced with CAST(GETDATE() AS Date). Am I missing something here?

CodePudding user response:

The intention is to get the date for at midnight to the time 10 minutes ago. So for (roughly) now, 2021-10-11T22:30:30 (UTC) this would return 2021-10-11T00:00:00. If, however, it were 2021-10-11T00:07:30 it would return 2021-10-10T00:00:00.

Why it doesn't just CAST/CONVERT to a date, I am not sure. Perhaps it was written before 2008 initially?

SELECT CONVERT(date, DATEADD(minute,-10, GETDATE());
  • Related