How to bring only results from the last 30 minutes

2

I have a table in sqlserver that will log error logs which contains the following fields:

sequence \ data \ message

I would like to know how to get the results of the last 30 minutes according to the current date that the example job runs:

date: 08/10/2014 10:00       10:08/2014 09:30

Assuming the job will run at 10:30 it should bring only the error that occurred at 10:00

How can I set up a query to get this result?

Note: I have already been able to mount the code that will put this record into a table. I just need to extract the records from the last 30 minutes each time it is run.

    
asked by anonymous 08.10.2014 / 15:21

1 answer

4

Using DATEADD() :

select dateadd(minute, -30, getdate())

In your case, your query will look like this:

select *
from tabela
where data >= dateadd(minute, -30, getdate())

Example in SQLFiddle

EDIT:

More information about functions that handle dates and time in T-SQL here .

(Thanks Motta for the link).

    
08.10.2014 / 15:31