如何获得今年上个月的记录,

时间:2013-07-22 05:15:58

标签: sql-server-2008-r2

我需要获取今年上个月的记录,但我也得到了过去几年的记录。请帮帮我

我有这样的查询:

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate()))
and empcode='13658'

GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;

1 个答案:

答案 0 :(得分:1)

您还需要检查年份条件。

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE 
DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(y, EventDate) = DATEPART(y, DATEADD(m, -1, getdate()))
and empcode='13658'

GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;