how to get data of current week only in SQL server?
datepart(dw, getdate())
is the current day of the week, dateadd(day, 1-datepart(dw, getdate()), getdate())
should be the first day of the week, add 7 to it to get the last day of the week
A better way would be
select datepart(ww, getdate()) as CurrentWeek
You can also use wk instead of ww.
Datepart Documentation
Do it like this:
SET DATEFIRST 1 -- Define beginning of week as Monday
SELECT [...]
AND WorkDate >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND WorkDate < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
Explanation:
datepart(dw, getdate())
will return the number of the day in the current week, from 1 to 7, starting with whatever you specified using SET DATEFIRST.dateadd(day, 1-datepart(dw, getdate()), getdate())
subtracts the necessary number of days to reach the beginning of the current weekCONVERT(date,getdate())
is used to remove the time portion of GETDATE(), because you want data beginning at midnight.
Its Working For Me.
Select * From Acb Where WorkDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND DATEADD(DAY, 1, GETDATE())
You have to put this line After the AND Clause AND DATEADD(DAY, 1, GETDATE())