soundy
soundy

Reputation: 307

How to group datas by week in sql?

i'm getting table values like below table

 Name            City             Date
  a                1             Mar 22 2013
  b                2             Apr 19 2013
  c                3             Apr 20 2013 
  d                4             Apr 22 2013
  e                5             Apr 27 2013
  f                6             Apr 30 2013
  g                7             Jun 5  2013

i got values like above table. Todays date is Apr 25 2013. i need to group by those values from +7 and -7 like Apr 18 2013 to Jun 2 2013. Please help me to do that.

Upvotes: 3

Views: 95

Answers (2)

Oleksandr Fedorenko
Oleksandr Fedorenko

Reputation: 16904

Depending on the current date is created dynamically condition

SELECT *
FROM dbo.yourTable
WHERE Date >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-7, 0)
  AND Date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())+8, 0)

Upvotes: 3

valex
valex

Reputation: 24144

select * from yourTable 
where DATEDIFF(day,'2013-04-25',[Date]) between -7 and 7

If you need the current date when query is running then use GETDATE() function instead of '2013-04-25'

Upvotes: 3

Related Questions