WadeB
WadeB

Reputation: 89

Determine length of days between two dates and display as number in SQL Server

I am trying to figure out how to get the number of days between these two days shown in a separate column called "Days" I tried using the DATEDIFF function but could not seem to get that to work. Any suggestions? the query works fine otherwise to accomplish all my other needs.

SELECT          
    Orders.OrderID,
    Customers.CompanyName,
    (OrderDetails.Quantity * OrderDetails.UnitPrice) AS OrderCost,
    FORMAT(Orders.ShippedDate, 'MMM dd yyyy') AS ShippedDate
FROM
    Customers 
INNER JOIN
    Orders ON Customers.CustomerID = Orders.CustomerID 
INNER JOIN
    OrderDetails ON Orders.OrderID = OrderDetails.OrderID
WHERE 
    ShippedDate >= '1994-01-01' AND ShippedDate <= '1994-01-31' 
ORDER BY 
    Orders.OrderID, CompanyName, ShippedDate, Orders.OrderDate

Here is what I am trying to achieve:

enter image description here

And here is my current result from the query:

enter image description here

Upvotes: 0

Views: 292

Answers (1)

Lloyd Moore
Lloyd Moore

Reputation: 3197

Try adding the following to the select clause ... DATEDIFF(DAY, ShippedDate, Orders.OrderDate) AS days

Upvotes: 1

Related Questions