Attempt to show a datetime of more than ten days

I am trying to create an SQL statement that calculates how many days delivery of under-delivered products is delayed relative to the current date. As a result, the order number, order date, product number and number of days of delay for order lines, where the number of days of delay exceeds 10 days, must be indicated.

Here is my SQL statement:

SELECT Orderhuvuden.ordernr, orderdatum, Orderrader.produktnr, datediff(day, orderdatum, isnull(utdatum, getdate())) as 'Delay days' FROM Orderhuvuden JOIN Orderrader ON Orderhuvuden.ordernr = Orderrader.ordernr AND utdatum IS NULL 

I have a problem with how to show delayed days in excess of 10 days. I tried to add something like:

 WHERE (getdate() - orderdatum) > 10 

But that will not work. Does anyone know how to solve this last step?

+5
source share
3 answers

Add this to your where clause:

 AND DATEDIFF(day, orderdatum, getdate()) > 10 
+9
source

I think you need to add this to the WHERE clause.

 datediff(day, orderdatum, isnull(utdatum, getdate())) > 10 
+1
source

If you need a condition:

 WHERE (getdate()-orderdatum) > 10 

Just rewrite this as:

 WHERE orderdatum < getdate() - 10 

Or:

 WHERE orderdatum < dateadd(day, -10, getdate()) 

It is also a "sargable" value than the index for orderdatum can be used to query.

+1
source

Source: https://habr.com/ru/post/1208534/


All Articles