Solved

how to get "Hours" in MS SQL

Posted on 2006-11-09
6
3,319 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:Scott Pletcher
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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:
Scott Pletcher 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 44
Sql Server group by 10 27
Inserting oldest record into new table. 5 24
How to resolve SQL Server DB deadlock which makes my application hangs ? 6 28
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

777 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