Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last 5 minutes of data with datetime data type in SQL Server

I would like to run a query against data that is being consumed by an Esri ArcGIS Server SQL Server database, the data reads as 4/15/2015 4:21:45 PM when I use

SELECT * 
FROM ServiceRequest.DBO.History_Table 
WHERE: Time = CONVERT(DATE, GETDATE())

I return all records with today's date, but how would I extend this so that I retrieve the last 5 minutes? My History_Table column is a date data type.

like image 776
hgs Avatar asked Apr 16 '15 00:04

hgs


People also ask

How do I get last 10 minutes records in SQL Server?

Here's the SQL query to select records for last 10 minutes. In the above query we select those records where order_date falls after a past interval of 10 minutes. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 10 minutes in the past.

How do I get last 30 minutes data in SQL?

SQL Server uses Julian dates so your 30 means "30 calendar days". getdate() - 0.02083 means "30 minutes ago".

How can I get last 5 records from a table?

METHOD 1 : Using LIMIT clause in descending order As we know that LIMIT clause gives the no. of specified rows from specifies row. We will retrieve last 5 rows in descending order using LIMIT and ORDER BY clauses and finally make the resultant rows ascending.


Video Answer


1 Answers

You should query from the parameter less five minutes using the DATEADD function

https://msdn.microsoft.com/en-us/library/ms186819.aspx?f=255&MSPPError=-2147217396

Then use the minutes parameter

WHERE [dateColumn] > DATEADD(minute, -5,  GETUTCDATE())
like image 114
Matthew Avatar answered Oct 13 '22 01:10

Matthew