?
Solved

SQL Time Difference and Comparison

Posted on 2014-02-05
1
Medium Priority
?
603 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
[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
1 Comment
 
LVL 25

Accepted Solution

by:
chaau earned 1500 total points
ID: 39837509
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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