Solved

Convert datetime into hours

Posted on 2011-09-12
3
232 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Backup & Restore 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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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