Solved

Convert datetime into hours

Posted on 2011-09-12
3
212 Views
Last Modified: 2012-05-12
Hi,

Below query returns if a backup hadn't occured in last 7 days. But my reuirement is to check for last 24 hours backup status. Please let me know, how to convert below query to check last 24 hours or 12 hours  or 6 hours back backup status.

SELECT  (SELECT   RTRIM(CONVERT(char(20), SERVERPROPERTY('machinename'))) + '_' + CONVERT(char(20), SERVERPROPERTY('instancename'))) as instance,
        sd.name as name,
        bs.type,
        bs.database_name,
        max(bs.backup_start_date) as last_backup,
        note = CASE
   WHEN max(bs.backup_start_date) < GETDATE() - 7 THEN 'ALERT'
   WHEN ISNULL(max(bs.backup_start_date),0) = 0 THEN 'ALERT'
   ELSE '---'
   END
FROM    master..sysdatabases sd
        Left outer join msdb..backupset bs on rtrim(bs.database_name) = rtrim(sd.name)
        left outer JOIN msdb..backupmediafamily bmf ON bs.media_set_id = bmf.media_set_id
        WHERE sd.name NOT LIKE '%tempdb%'
Group by sd.name,
        bs.type,
        bs.database_name
Order by sd.name,last_backup


Thanks in Advance.

0
Comment
Question by:sg05121983
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36524070
You can use DATEADD(HH, -24, GETDATE()) to check 24 hours ago.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 36524083
And consequently, you can replace -24 with whatever amount of hours less or more than the passed in date you want to use. So for example, 12 hours ago is DATEADD(HH, -12, GETDATE()).
0
 

Author Closing Comment

by:sg05121983
ID: 36534068
--
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

20 Experts available now in Live!

Get 1:1 Help Now