DATEs in SQL
DATEADD(): Add or subtract datetime values and the result is a date
DATEADD(DATEPART, number, date]
DATEPART: Unit of measurement (DD,MM etc) number: An integer value to add or subtract date: A datetime value
-- Return the DeliveryDate as 5 days after the ShipDate
SELECT OrderDate,
DATEADD(DD, 5, ShipDate) AS DeliveryDate
FROM Shipments
DATEDIFF(): takes the difference between two date times and returns a number
DATEDIFF(datepart , startdate, enddate)
datepart: Unit of measurement (DD,MM etc) startdate: The start date value enddate:The end date value
SELECT OrderDate, ShipDate,
DATEDIFF(DD, OrderDate, ShipDate) AS Duration
FROM Shipments