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

Filter query base on date field (3d)

Pull results from a stored procedure (3d)

Help With Query To Count Number of Pets & Vaccinations (4d)

Connect sql off network (4d)

Moving Registered Servers to a New Instance (8d)

CU 31 for 2017 Will Not Install (8d)

I get different results when I scan from a barcode reader with a different language (10d)

Spatial query in SQL server (12d)

- Advertisement -