How to bring only results from the last 30 minutes

Asked

Viewed 3,962 times

2

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

sequence date message

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

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

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

How can I mount a query to get this result.

Note: I’ve already been able to mount the code that will put this record in a table I really only need to extract the records from the last 30 minutes each time it runs.

  • 3

    use functions such as DATEADD E GETDATE http://technet.microsoft.com/en-us/library/aa258863%28v=sql.80%29.aspx

  • Thank you Motta, perfect your explanation and solution.

  • No, the Omni who gave the solution I just pointed to the source.

  • @Motta we put at the same time. I will edit the answer and put your link to anyone who wants to deepen the knowledge about these functions.

1 answer

4


Using DATEADD():

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

In your case your query will be something like:

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

Example in Sqlfiddle

EDIT:

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

(Thank you Motta by the link).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.