SQL Server Sorting By Multiple Columns

08/19/11

Today I have asked a question from a friend of mine developing with sql server and converting some oracle queries to mssql format.
Here is oracle query: SELECT * FROM HIZMETBELGESI WHERE PERSONELID = @PERSONELID ORDER BY HAREKETTARIHI,BITISTARIHI NULLS FIRST
I have googled and find these:
Firstly you can sort by multiple columns by stating each column in the ORDER BY clause, separating each column name with a comma. SQL will first order the results by the first column, then the second, and so on for as many columns that are included in the ORDER BY clause.
SELECT * FROM HIZMETBELGESI ORDER BY HAREKETTARIHI, BITISTARIHI
Secondly, You can use case for nulls first;
ORDER BY (CASE WHEN BITISTARIHI IS NULL THEN 1 ELSE 0 END) DESC, BITISTARIHI DESC
And last to say Oracle seems better in this case.
Happy querying..

Yorumlar

Görüşünüzü paylaşın

(No HTML, Textile help only please.)