Comparing Dates

By Bill Graziano on 21 August 2000 | Tags: Queries


sandy writes "hello team, suppose i were to store a slew of future date/time values in a table.
now, as the actual date/time approaches some of my stored values, how can i select only records that are within say 15 minutes of the actual time through a query on lets say an asp page that refreshes itself every 15 minutes.

hope i can't stump u. [hope u can understand my question] thanks omnipotent 1s"
We hope you can't stump us either :)

Ok Sandy, you get bonus points for the omnipotent part. We SQLTeamer's aren't above letting a little flattery affect us :) The function you are looking for is called DATEDIFF. It compares two dates and returns the results in the unit of measure you specify. Your syntax might look something like this:

SELECT foo
FROM table
WHERE DATEDIFF(minute, TableDate, getdate() ) <= 15


We want the results in minutes. You can also return it in years, days, months, hours, seconds, etc. The first date (TableDate) is subtracted from the second date. I used the system date for the second date. This query will tell you how many minutes from TableDate the system date is. If it's within 15 minutes we display the record.


Related Articles

Using Dynamic SQL in Stored Procedures (7 March 2011)

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

Aggregating Correlated Sub-Queries (23 October 2007)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

Returning Complex Data from User-Defined Functions with CROSS APPLY (11 June 2007)

Returning a week number for any given date and starting fiscal month (2 May 2007)

Other Recent Forum Posts

My informix Sql query retruns Null always (7h)

Vehicle availability query (1d)

SSDT - Unable to reference 'master' or 'msdb' with new sdk style project (1d)

Ola Hallengren backup jobs (1d)

Compare alpha results to INT after get values from a string (5d)

Query performance Call Center data (6d)

Looking for on Premises tool to read data from SQL logs and populate data warehouse (6d)

Possible SQL 2014 to 2016 Issue - Some Application Functions Slow/Failing (7d)

- Advertisement -