31

I want records from table which stores the current date when a record is inserted with in current week only.

I have tried:

SELECT PId 
,WorkDate 
,Hours          
,EmpId            
FROM Acb       
WHERE EmpId=@EmpId AND WorkDate BETWEEN DATEADD(DAY, -7, GETDATE()) AND GETDATE()
5
  • 1
    What went wrong with your query? Apr 14, 2014 at 2:51
  • It is showing record from today's date to last 7 days [email protected]
    – sona
    Apr 14, 2014 at 2:58
  • Define "current week". Does it start on Sunday or Monday? Apr 14, 2014 at 3:15
  • Try changing to -6 instead. Apr 14, 2014 at 3:34
  • Current week starts from monday@Greenstone walker
    – sona
    Apr 14, 2014 at 4:48

7 Answers 7

47

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 week
  • CONVERT(date,getdate()) is used to remove the time portion of GETDATE(), because you want data beginning at midnight.
2
  • What might I do if I wanted to use my own date object rather the predefined getdate()? Jan 4, 2020 at 15:14
  • Thank you very much. Important to note that, SET DATEFIRST 1 reverts back to its original state after stored procedure call.
    – orBeat
    Jun 24, 2021 at 15:13
11

A better way would be

select datepart(ww, getdate()) as CurrentWeek

You can also use wk instead of ww.

Datepart Documentation

4
  • 2
    I know this is old (just found via Google), but wouldn't this also return records with dates in the same week, but from different years?
    – 3N1GM4
    Jan 25, 2017 at 11:19
  • duplicate answer - answered Apr 14 '14 at 12:53 Amul Harad
    – AquaAlex
    Jan 10, 2020 at 14:46
  • @AquaAlex they are not duplicates. Jan 13, 2020 at 10:48
  • keep in mind for the first week of the year for example 2019-12-31 and 2020-01-01 are in the same week but datepart(ww, getdate()) will return 53 and 1
    – Nerdroid
    Oct 8, 2021 at 0:04
2

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())

1
  • Your retrieved data is not for the current week, you are getting data that is a week ago.
    – AquaAlex
    Jan 10, 2020 at 14:43
1

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

0

You can use following query to extract current week:

select datepart(dw, getdate()) as CurrentWeek
1
  • Doesn't this return the current day of the week, rather than the current week of the year?
    – 3N1GM4
    Jan 25, 2017 at 11:21
0
SET DATEFIRST 1;
    ;With CTE
    AS
    (
    SELECT
    FORMAT(CreatedDate, 'MMMM-yyyy') as Months,
    CASE 
    WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(CreatedDate)), Min(CreatedDate))) < YEAR(Min(CreatedDate))
    THEN FORMAT(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) ,'MMM dd')   + ' - ' + FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(CreatedDate))), Min(CreatedDate))  ,'MMM dd')
    ELSE
    FORMAT(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(CreatedDate)), Min(CreatedDate)) ,'MMM dd') + ' - ' + FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(CreatedDate))), Min(CreatedDate)) ,'MMM dd') 
    END  DateRange,
    Sum(ISNULL(Total,0)) AS Total,
    sum(cast(Duration as int)) as   Duration    
    FROM TL_VriandOPI_Vendorbilling where VendorId=@userID and CompanyId=@CompanyID
    Group By DATEPART(wk, CreatedDate) ,FORMAT(CreatedDate, 'MMMM-yyyy')
    )
    SELECT Months,DateRange,Total,Duration,
    case when DateRange=(select FORMAT(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min(getdate())), Min(getdate())) ,'MMM dd') + ' - ' +
    FORMAT(DATEADD(dd, 7-(DATEPART(dw, Min(getdate()))), Min(getdate())) ,'MMM dd'))
    then 1 else 0 end as Thisweek
    FROM CTE order by Months desc
0

Using DATEDIFF works as well, however a bit hacky since it doesn't care about datefirst:

set datefirst 1; -- set monday as first day of week
declare @Now datetime = '2020-09-28 11:00';


select * 
into #Temp
from
(select 1 as Nbr, '2020-09-22 10:00' as Created
union
select 2 as Nbr, '2020-09-25 10:00' as Created
union
select 2 as Nbr, '2020-09-28 10:00' as Created) t


select * from #Temp where DATEDIFF(ww, dateadd(dd, -@@datefirst, Created), dateadd(dd, -@@datefirst, @Now)) = 0 -- returns 1 result
select * from #Temp where DATEDIFF(ww, dateadd(dd, -@@datefirst, Created), dateadd(dd, -@@datefirst, @Now)) = 1 -- returns 2 results


drop table #Temp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.