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

AlwaysOn AG + Replication maintenance - two scenarios to get the job done (19h)

What happens in a dual LEFT OUTER join when the second join is NULL in both tables? (20h)

How to set a variable from a table with comma? (1d)

SSRS Expression IIF Zero then ... Got #Error (2d)

Understanding 2 Left Joins in same query (3d)

Use a C# SQLReader to input an SQL hierarchyid (3d)

Translate into easier query/more understandable (3d)

Aggregation view with Min and Max (4d)

- Advertisement -