Hearty
Hearty

Reputation: 85

How to get records of last month of this year,

I need to fetch records of last month of this year, but i am getting records of past years also. please help me out

I had a query like this:

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;

Upvotes: 1

Views: 491

Answers (1)

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

You need to check year condition as well.

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;

Upvotes: 1

Related Questions