SQL Server get records before a defined time
I am trying开发者_JAVA技巧 to get a list of records that have been in the system only before 05:00:00 pm of the day the query is being run. I have this query that works in the basic testing, but I wanted to see if there is a better option than to do the SQL concatenation.
select *
from Payment
where createTimestamp <= CONVERT(VARCHAR(10), Getdate(), 120) +' '+'17:00:00';
This query will be used on SQL Server 2008.
Select ...
From Payment
Where CreateTimeStamp <= DateAdd(hh, 17, DateDiff(d, 0, CURRENT_TIMESTAMP))
Another option given that you are using SQL Server 2008
Select ...
From Payment
Where CreateTimeStamp <= DateAdd(hh, 17, Cast(Cast(CURRENT_TIMESTAMP As Date) As DateTime))
This will retrieve all records with a createTimestamp
between 12am and 5pm today.
select *
from Payment
where createTimestamp
between dateadd(dd, datediff(dd, 0, getdate()), 0)
and dateadd(hh, 17, dateadd(dd, datediff(dd, 0, getdate()), 0))
This will retrieve all records with a createTimestamp
between 5pm yesterday and 5pm today.
select *
from Payment
where createTimestamp
between dateadd(hh, -7, dateadd(dd, datediff(dd, 0, getdate()), 0))
and dateadd(hh, 17, dateadd(dd, datediff(dd, 0, getdate()), 0))
For more fun with sql dates check out my answer to GETDATE last month.
精彩评论