Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,
I have a table called tblactivity,where there is a column called checkintime.
Now I need to find all the checkin time which is within last 2 hours of the current time.
thanks in advance.
Posted

TRY THIS QUERY...

SQL
SELECT * FROM tblactivity where DATEDIFF(hour,checkintime,GETDATE()) <= 2


HOPE IT HELPS...
 
Share this answer
 
Comments
avishekrc 30-Nov-11 4:27am    
Hi Karthik,
Thanks,but here is another catch:
In my tblactivity the checkintime colm values are stored as '01:55 PM', '02:20 AM'etc. so it's not giving the correct data.
check following Query
SQL
SELECT * FROM tblactivity WHERE checkintime BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR)
 
Share this answer
 
Comments
avishekrc 30-Nov-11 4:29am    
'Now' is not a recognized function name.I am using sqlserver 2005.May be its avaliable in 2008.
koolprasad2003 30-Nov-11 4:37am    
SELECT * FROM tblactivity WHERE checkintime BETWEEN DATE_SUB(GETDATE(), INTERVAL 2 HOUR)
avishekrc 30-Nov-11 4:42am    
Incorrect syntax near '2'
Here it is :

SQL
select * from tblactivity where checkintime > DATEADD(HOUR, -2, GETDATE()) 


Good Luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900