Solved

check if time is more than 24 hours

Posted on 2014-10-17
4
109 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
4 Comments
 
LVL 39

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 65

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:ScottPletcher
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now