Solved

SQL Time Difference and Comparison

Posted on 2014-02-05
1
585 Views
Last Modified: 2014-02-05
Hello Experts,

I am currently developing a script that will compare times and get the time difference using the following script.

declare @update_time datetime
declare @current_time datetime
--declare @inactive varchar(1)
--variable to hold y/n value if time difference indicates table to be inactive

set @update_time = (
SELECT last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'NGDemo')
AND OBJECT_ID=OBJECT_ID('aa_test')
)

set @current_time = GETDATE()

select convert(varchar(5),DateDiff(s, @update_time, @current_time)/3600)+':'+
convert(varchar(5),DateDiff(s, @update_time, @current_time)%3600/60)+
':'+convert(varchar(5),(DateDiff(s, @update_time, @current_time)%60)) as [hh:mm:ss]

Open in new window


Would it be possible to set the @inactive variable to 'Y' if the time difference is greater than say 1 hour?
0
Comment
Question by:robthomas09
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
If I understood you correctly, this is what you are after:
declare @update_time datetime
declare @current_time datetime
declare @inactive varchar(1)
--variable to hold y/n value if time difference indicates table to be inactive

set @update_time = (
SELECT last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'NGDemo')
AND OBJECT_ID=OBJECT_ID('aa_test')
)

set @current_time = GETDATE()

select convert(varchar(5),DateDiff(s, @update_time, @current_time)/3600)+':'+
convert(varchar(5),DateDiff(s, @update_time, @current_time)%3600/60)+
':'+convert(varchar(5),(DateDiff(s, @update_time, @current_time)%60)) as [hh:mm:ss],
@inactive = CASE WHEN DateDiff(s, @update_time, @current_time) >= 3600 THEN 'Y' ELSE 'N' END

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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

17 Experts available now in Live!

Get 1:1 Help Now