?
Solved

getdate() question

Posted on 2007-11-19
4
Medium Priority
?
1,150 Views
Last Modified: 2008-02-01
I've created a views that will list all the products- ProductID and quantity, that were ordered (OrderDate) in last twenty four hours between getdate()-1 and getdate().


create view ProductsSold
as
select orderdetails.ProductID, orderdetails.OrderQty, orders.OrderDate
from OrderDetails inner join Orders
on OrderDetails.salesOrderID = Orders.salesOrderID
where OrderDate > dateadd(d, -1, getdate()) and OrderDate < getdate()

I also tried these:
 Where datediff(hh,orderdate,getdate()) <= 24
Where datediff(d,orderdate,getdate()) = 1

but they all produced different results. I thought they would produced the same results.
is it possible to say: where OrderDate between getdate()-1 and getdate()
0
Comment
Question by:salesprod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 1600 total points
ID: 20316823
yes you can just use

where OrderDate between getdate()-1 and getdate()

the differences:
1.Where datediff(hh,orderdate,getdate()) <= 24
a.datediff HH returns only the difference between the hours, and ignores minutes. so you get some more orders creeping into the result set

2.Where datediff(d,orderdate,getdate()) = 1
a.same as above. datediff(d, '2007-01-01', '2007-01-02 23:59:59') is 1
0
 

Author Comment

by:salesprod
ID: 20316842
no wonder there were more results displayed. That helps! thanks!
so just to confirm it will return results in the last 24 hours?
0
 
LVL 4

Assisted Solution

by:moondist
moondist earned 400 total points
ID: 20316856
I think this will work for you..

select orderdetails.ProductID, orderdetails.OrderQty, orders.OrderDate
from OrderDetails inner join Orders
on OrderDetails.salesOrderID = Orders.salesOrderID
where OrderDate > DATEADD(hh,-24,getdate())

0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 1600 total points
ID: 20316870
yes, between getdate()-1 and getdate() will give you 24 hours
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

770 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