how to get data of current week only in SQL server?

前端 未结 7 1936
逝去的感伤
逝去的感伤 2020-12-29 03:18

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 
,Ho         


        
相关标签:
7条回答
  • 2020-12-29 03:52

    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.
    0 讨论(0)
  • 2020-12-29 03:55

    A better way would be

    select datepart(ww, getdate()) as CurrentWeek
    

    You can also use wk instead of ww.

    Datepart Documentation

    0 讨论(0)
  • 2020-12-29 03:55

    You can use following query to extract current week:

    select datepart(dw, getdate()) as CurrentWeek
    
    0 讨论(0)
  • 2020-12-29 03:58
    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 讨论(0)
  • 2020-12-29 03:58

    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
    
    0 讨论(0)
  • 2020-12-29 04:01

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

    0 讨论(0)
提交回复
热议问题