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

Query performance Call Center data (21h)

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

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

Working with multiple WHERE statements (1d)

Create a new field value that shows a difference in value from 2 fields (3d)

Hierarchy wise Sales Targets (3d)

Get the MaxDate in results (5d)

Switch Statement within a SSRS SQL Expression (6d)

- Advertisement -