Check date between two dates in T-SQL

GeoVIP picture GeoVIP · Sep 10, 2013 · Viewed 81.1k times · Source

I have a stored procedure where want check that a date is between a fixed date and the current date/time (with GETDATE()):

SELECT
    a, b
FROM myTbl
WHERE
    DATE BETWEEN 'Day start datetime' AND GETDATE()

...for example :

WHERE
    DATE BETWEEN '2013-09-10 00:00:00.00' AND 'GETDATE()'

How to do it?

Answer

Damien_The_Unbeliever picture Damien_The_Unbeliever · Sep 10, 2013

A pair of DATEADD/DATEDIFF calls will round a date down to the previous midnight:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN DATEADD(day,DATEDIFF(day,0,GETDATE()),0) and GETDATE()

Alternatively, if you're on SQL Server 2008 or later:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN CONVERT(date,GETDATE()) and GETDATE()