Solved

select previous date - 4 pm to 4 pm

Posted on 2011-02-22
2
290 Views
Last Modified: 2012-08-13
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
Comment
Question by:VBdotnet2005
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 125 total points
ID: 34957022
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 34957117
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question