As an example, the following query returns for each order the order ID and shipped date,
ordered by the latter.
SELECT orderid, shippeddate
FROM Sales.Orders
WHERE custid = 20
ORDER BY shippeddate;
Remember that unshipped orders have a NULL in the shippeddate column; hence, they
sort before shipped orders, as the query output shows.
orderid shippeddate
----------- -----------------------
11008 NULL
11072 NULL
10258 2006-07-23 00:00:00.000
10263 2006-07-31 00:00:00.000
10351 2006-11-20 00:00:00.000
...
Standard SQL supports the options NULLS FIRST and NULLS LAST to control how NULLs
sort, but T-SQL doesn’t support this option. As an interesting challenge, see if you can figure
out how to sort the orders by shipped date ascending, but have NULLs sort last. (Hint: You
can specify expressions in the ORDER BY clause; think of how to use the CASE expression to
achieve this task.)
This is what i have so far
use TSQL2012
select orderid, shippeddate
from sales.orders
where custid = 20
order by case
when shippeddate is null then shippeddate end desc;
What am i missing
Thanks