Solved

how to get "Hours" in MS SQL

Posted on 2006-11-09
6
3,321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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