Solved

how to get "Hours" in MS SQL

Posted on 2006-11-09
6
3,318 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
ID: 17908777
DATEADD(MINUTE, -5, GETDATE())  , etc.
0
 

Author Comment

by:ezzadin
ID: 17910163
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
ID: 17910247
Select *
from session
where sessionstart = DATEADD(MINUTE, -5, GETDATE())  
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17910524
>> 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
ID: 17910583
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
ID: 17915045
Hi Scott,

your query worked.

Thanks

Thanks Pai and acperkins
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

896 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