Fetching all records for the previous day in SQL Server

Here's a query that lets you fetch all records for the previous day that falls between the time duration of 12 to 12


USE Northwind


DECLARE @PrevDate AS DateTime


SET @PrevDate = '1996-08-15'


SELECT OrderID, CustomerID, EmployeeID, OrderDate


FROM ORDERS


WHERE OrderDate < Cast(Convert(varchar,@PrevDate,101) as datetime)


and OrderDate >=  Cast(Convert(varchar,@PrevDate,101) as datetime) - 1




Results:


10280    BERGS    2    1996-08-14 00:00:00.000


10281    ROMEY    4    1996-08-14 00:00:00.000


No comments:

Post a Comment