• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

select previous date - 4 pm to 4 pm

I want to query from my table(MS SQL) to select orderdate from lastdate @ 4 pm up to currentdate @ 4pm. What would be the query statement?

select orderdate from mytable where datediff(dd, orderdate, getdate()) = 1 ??? how about 4pm to 4 pm?
0
VBdotnet2005
Asked:
VBdotnet2005
1 Solution
 
SharathData EngineerCommented:
check this.
select orderdate 
  from mytable 
 where orderdate between DATEADD(dd,0,datediff(dd,0,GETDATE()-1))+'16:00:00' and DATEADD(dd,0,datediff(dd,0,GETDATE()))+'16:00:00'

Open in new window

0
 
Ephraim WangoyaCommented:
You can calculate start and end date prior to the query to make it a bit faster
DECLARE @STARTDATE DATETIME, @ENDDATE DATETIME

SET @STARTDATE = DATEADD(DAY, -1, DATEDIFF(DAY, 0, GETDATE())) + '16:00'
SET @ENDDATE = DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) + '16:00'

select orderdate 
from mytable 
where orderdate between @STARTDATE AND @ENDDATE

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now