Check date between two dates in T-SQL

85,535

Solution 1

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()

Solution 2

'GETDATE()' is a string literal, GETDATE() is a T-SQL function.

Your query should look like:

SELECT a , b
FROM myTbl
WHERE DATE BETWEEN '2013-09-10 00:00:00.0' and GETDATE()

Solution 3

I think WHERE DATE BETWEEN '2013-09-10 00:00:00.00' and GETDATE() (without the single quotes around the GETDATE() call) should work just fine.

Share:
85,535
GeoVIP
Author by

GeoVIP

Updated on July 09, 2022

Comments

  • GeoVIP
    GeoVIP almost 2 years

    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?

  • GeoVIP
    GeoVIP almost 11 years
    '2013-09-10 00:00:00.00' this is for example , tomorrow it will be dont correctly work , I nedd current date start time
  • Onkel Toob
    Onkel Toob almost 11 years
    Ok, I didn't get that from the question. The correct answer is given by @Damien_The_Unbeliever then.