Case Order by using Null

Posted by molgan on Stack Overflow See other posts from Stack Overflow or by molgan
Published on 2010-03-23T07:35:54Z Indexed on 2010/03/23 7:43 UTC
Read the original article Hit count: 317

Filed under:
|
|

Hello

I have the following test-code:

CREATE TABLE #Foo (Foo int)

INSERT INTO #Foo SELECT 4 INSERT INTO #Foo SELECT NULL INSERT INTO #Foo SELECT 2 INSERT INTO #Foo SELECT 5 INSERT INTO #Foo SELECT 1

SELECT * FROM #Foo ORDER BY CASE WHEN Foo IS NULL THEN Foo DESC ELSE Foo END

DROP TABLE #Foo

I'm trying to produce the following output:

1 2 3 4 5 NULL

"If null then put it last"

How is that done using Sql 2005

/M

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-2005