Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to get "Hours" in MS SQL

Posted on 2006-11-09
6
Medium Priority
?
3,324 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 70

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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
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 70

Accepted Solution

by:
Scott Pletcher earned 500 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

688 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