Solved

check if time is more than 24 hours

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Weekly Schedule 15 28
TSQL - How to declare table name 26 30
Sql Server group by 10 27
PROPERCASE SCRIPT IN SQL 3 6
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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