我需要从sqlserver2008r2获取记录,我查询当前周的记录是:
select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from
TMS_HOURCALC WHERE intime1
BETWEEN dateadd(dd,(datediff(DD,-53684,getdate())/7)*7,-53684)
AND dateadd(dd,((datediff(dd,-53684,getdate())/7)*7)+7,-53684)
and empcode = @empcode
GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1
order by intime1;
任何人都可以帮助我从上周日到上周六获取上周的记录。
答案 0 :(得分:2)
对列进行计算并在where子句中引用它将会产生糟糕的性能。这是一个更好的方法:
select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from
TMS_HOURCALC
WHERE intime1 >= dateadd(dd,(datediff(DD,-1,getdate())/7)*7-7,-1)
and intime1 < dateadd(dd,((datediff(dd,-1,getdate())/7)*7),-1)
and empcode = @empcode
GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1
order by intime1;
答案 1 :(得分:1)
确保您的数据库@@DateFirst设置为7(星期日默认值)并使用DATEPART()
尝试使用:
where
DATEPART(ww, intime1) = DATEPART(ww, GetDate())
and
YEAR(intime1) = YEAR(GetDate())