Solved

select previous date - 4 pm to 4 pm

Posted on 2011-02-22
2
261 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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now