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?
SQL Server DATEDIFF() Function The DATEDIFF() function returns the difference between two dates.
How do I count days between two dates in SQL Server? To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function.
The time is in a 24-hour format. Syntax: SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';
SELECT * FROM YourTable. WHERE [dateColumn] >DATEADD(day,1,'4/25/2022') AND [dateColumn] <= DATEADD(day,1,'4/26/2022') AND DATEPART(hh,[dateColumn]) >= 7 AND DATEPART(hh,[dateColumn]) <= 19.
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()
'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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With