Find previous date algorithm

I found a couple of examples on the Internet, but I do not quite understand how this works. eg

SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0) 

I'm not even sure that he is doing what I want.

What I need, when the query is executed, it can SELECT * from xTable WHERE xDate be between two dates. Last Sunday and next Sunday (current week). What can I use for automatic search? And explain, because I'm new to SQL.

+6
source share
1 answer

I looked deeper, trying to understand this request

  SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0) 

And used it to restore the previous Monday. Now I can just DATEADD another 6 days to get a full week.

The solution I used:

  Set @Monday = DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0) Set @Sunday = DATEADD(dd, 06, @Monday) 

The problem is resolved.

+4
source

Source: https://habr.com/ru/post/918473/


All Articles