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.
Author by
GeoVIP
Updated on July 09, 2022Comments
-
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 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 almost 11 yearsOk, I didn't get that from the question. The correct answer is given by @Damien_The_Unbeliever then.