Functions, Dates and Cleaning · Problem 21 of 118

Days to Ship

MediumFunctions, Dates and CleaningShop data

For the shipped orders (shipped_on is not NULL) with order_id from 1 to 12, return order_id and days_to_ship, the number of days between order_date and shipped_on as a whole number. Sort by order_id.

Hint 1

JULIANDAY(date) turns a date into a day number, so subtracting two of them gives days.

Hint 2

Wrap the result in CAST(... AS INTEGER) to remove the .0.

Reference solution

SELECT order_id,
       CAST(JULIANDAY(shipped_on) - JULIANDAY(order_date) AS INTEGER) AS days_to_ship
FROM orders
WHERE shipped_on IS NOT NULL
  AND order_id BETWEEN 1 AND 12
ORDER BY order_id;

Dates stored as text cannot be subtracted directly. JULIANDAY converts them to numbers (days since a fixed origin), so the difference of two of them is a number of days. An order that has not shipped has shipped_on = NULL, and any calculation with NULL gives NULL, so you filter those rows out or decide on purpose how to show them.