?
Solved

check if time is more than 24 hours

Posted on 2014-10-17
4
Medium Priority
?
122 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 200 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 1000 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 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

764 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