Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to show datediff greater than ten days

Tags:

sql

sql-server

I'm trying to create a SQL statement, which calculates how many days a delivery of undelivered products are delayed relative to the current date. The result should show the order number, order date, product number and the number of delay days for the order lines where the number of days of delay exceeds 10 days.

Here is my SQL statement so far:

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

What I have a problem with is to solve how to show the delayed days that exceeds 10 days. I've tried to add something like:

WHERE (getdate() - orderdatum) > 10

But it doesn't work. Does anyone know how to solve this last step?

like image 524
eqinna Avatar asked Dec 07 '14 16:12

eqinna


1 Answers

Add this to your where clause:

AND DATEDIFF(day, orderdatum, getdate()) > 10
like image 170
SMA Avatar answered Oct 05 '22 16:10

SMA