Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

check this

SQL
use TSQL2012
select orderid, shippeddate
from sales.orders
where custid = 20
order by case
when shippeddate is null  then 1 else 0 end, shippeddate ;
 
Share this answer
 
SQL
select orderid, shippeddate
from sales.orders
where custid = 20
order by 
	CASE
		WHEN shippeddate IS NULL  THEN shippeddate 
		ELSE 0 
	END
		 DESC, shippeddate ASC;
 
Share this answer
 
Comments
Maciej Los 4-Dec-14 14:06pm    
Please, delete this 'solution' and do not post answer to such as old questions!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900