Solved

how to get "Hours" in MS SQL

Posted on 2006-11-09
6
3,317 Views
Last Modified: 2008-01-09
hello,

in MS SQL we use Getdate() -1 to get yesterdays data, what if I need to get the last 5 min or the last 2 hours data.... is there something like Getime() -2 ?

thanks


0
Comment
Question by:ezzadin
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
DATEADD(MINUTE, -5, GETDATE())  , etc.
0
 

Author Comment

by:ezzadin
Comment Utility
hi Scott,

Thanks... well, can you help me with the query plz:


my table name is session, I need to return all session that were recorded 5 minutes ago, field name is sessionstart

Thanks

0
 
LVL 10

Expert Comment

by:pai_prasad
Comment Utility
Select *
from session
where sessionstart = DATEADD(MINUTE, -5, GETDATE())  
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>> I need to return all session that were recorded 5 minutes ago, field name is sessionstart <<
You don't actually mean exaclty 5 minutes ago, do you?
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
Comment Utility
For all sessions started within the last five minutes (5 mins ago, 4 mins ago, etc.):

SELECT *
FROM session
WHERE sessionStart >= DATEADD(MINUTE, -5, CONVERT(CHAR(16), GETDATE(), 120))


For all sessions started within the 5th min. ago only :
SELECT *
FROM session
WHERE sessionStart >= DATEADD(MINUTE, -5, CONVERT(CHAR(16), GETDATE(), 120)) AND sessionStart < DATEADD(MINUTE, -4, CONVERT(CHAR(16), GETDATE(), 120))
0
 

Author Comment

by:ezzadin
Comment Utility
Hi Scott,

your query worked.

Thanks

Thanks Pai and acperkins
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

771 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

11 Experts available now in Live!

Get 1:1 Help Now