Solved

select previous date - 4 pm to 4 pm

Posted on 2011-02-22
2
280 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 42
Addition to SQL for dynamic fields 6 37
Trying to get a Linked Server to Oracle DB working 21 57
MS SQL Delete Query 9 30
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

785 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