Solved

check if time is more than 24 hours

Posted on 2014-10-17
4
114 Views
Last Modified: 2014-10-21
hi experts

I have a table Customer which has columns

ID   Name   LastLoggedIn   LoggedInstatus

LastLoggedIn is a timestamp column


I want to come up with a sql which checks
if current time - LastLoggedIn > 24 hours  THEN
update Customer set LoggedInstatus = false

basically the point is if customer is logged in more than 24
hours set the LoggedInstatus  to false

trying to come up with sql , doesnt seem like working

update Customer set LoggedInstatus = false
where
select customer.LastLoggedIn - GETDATE()  from Customer customer  > 24

any help will be appreciated..

thanks.
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 50 total points
ID: 40387348
You will need to use a DATEDIFF function:

http://msdn.microsoft.com/en-us/library/ms189794.aspx

so will be something like:

--update Customer set LoggedInstatus = false
select * from  Customer
where
DATEDIFF ( hour , customer.LastLoggedIn , GETDATE() ) > 24


and this statement will show you the list of records to be updated is you run it against your database - note that I commented out the UPDATE to make sure that's what you want first
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 40387359
Give this a whirl..
UPDATE Customer 
SET LoggedInstatus = false
WHERE DateDiff(hour, LastLoggedIn, GETDATE()) > 24 

Open in new window

or better, so it doesn't compute GETDATE() multiple times
Declare @dt datetime = GETDATE()

UPDATE Customer 
SET LoggedInstatus = false
WHERE DateDiff(hour, LastLoggedIn, @dt) > 24 

Open in new window

0
 

Author Comment

by:royjayd
ID: 40387510
thanks, let me give it a twirl.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 40387633
1) NEVER use a function on a column in the WHERE clause if you can avoid it.
2) A computed column might be better for you in this case, as it would always show the current status.

1)
update Customer
set LoggedInstatus = 'false'
where
    LastLoggedIn < DATEADD(HOUR, -24, GETDATE())

2)
ALTER TABLE Customer
ADD LoggedInstatus AS CASE WHEN LastLoggedIn >= DATEADD(HOUR, -24, GETDATE()) THEN 'True' ELSE 'False' END
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question