0

I want to simply sort the table by the column "names" with NULL fields IN THE END.

I tried this:

SELECT *       
FROM Table 
ORDER BY (CASE
            WHEN Name IS NULL THEN 1 
            ELSE 0 
          END), 
         name

The problem is that there is more than 1 nullable column, so obviously the above code don't resolve the problem.

Jonhz
  • 115
  • 1
  • 2
  • 9

1 Answers1

0

You can use OR operator to join multiple columns

order by (isnull(column1) or isnull(column2) or ..)
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • @Jonhz, can you create a demo sqlfiddle? – Fabricator Jul 20 '14 at 04:45
  • I'll try to create a sqlfiddle. I think another solution would be to change all NULL's into empty ("").But how could I do that? – Jonhz Jul 20 '14 at 04:51
  • Thanks for the help, but not exactly what i need... I wanted to order by the column A and all NULL in A in the end, like this: http://s28.postimg.org/oemqe1g0d/image.jpg – Jonhz Jul 20 '14 at 05:09
  • @Jonhz, try `order by isnull(columnA), columnA` – Fabricator Jul 20 '14 at 05:10
  • oh maaaaaan i'm feeling so dumb right now.. i did tried that before but I was doing another thing wrong... i got it now, thanks! – Jonhz Jul 20 '14 at 05:49