Solved

check if time is more than 24 hours

Posted on 2014-10-17
4
107 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now