如何获得上周的记录?

时间:2014-06-16 09:03:52

标签: sql sql-server sql-server-2008

我能够获得当周的记录。

但我想得到上周的记录:

我的代码到现在为止:

select * from T1
where WorkDate >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))   
AND WorkDate <  dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))

2 个答案:

答案 0 :(得分:1)

我认为这对你有用[将表改为你的]

SELECT M.[mId],M.[mName],M.mInsertDate
    FROM dbo.TblMusic M 
    WHERE DATEPART(ww,M.[mInsertDate])=DATEPART(ww,GETDATE()-7)
    ORDER BY M.mInsertDate DESC

答案 1 :(得分:1)

declare @t table
(
    id int primary key,
    created_date date,
    data varchar(10)
  )

insert into @t values (1,'2012-01-01','testing');

SELECT Created_Date
FROM @t
WHERE Created_Date >= DATEADD(day,-11117, GETDATE())

SELECT Created_Date
FROM Table_Name
WHERE Created_Date >= DATEADD(day,-7, GETDATE())