2017-03-23 7 views

答えて

2
Select * 
From YourTable 
Order By case when DateCol is null then 1 else 0 end 
     ,DateCol 

あるいはOrder By IsNull(DateCol,'2525-12-31')

+1

「人がまだ生きているなら2525年に」? –

+0

@ZoharPeledようこそ!誰かがそれを得た –

+0

私はこれらの古い曲が大好きです... –

12

どこヌルを置くために指定することができます。

order by col asc nulls first 
order by col asc nulls last 
order by col desc nulls first 
order by col desc nulls last 

が、T-SQLは、ここでは、標準に準拠していません。 NULL値の順序は、ソート昇順またはT-SQLに下降するかどうかによって異なります。あなたは、単にソートネガによってできた整数で

order by col asc -- implies nulls first 
order by col desc -- implies nulls last 

order by -col asc -- sorts by +col desc, implies nulls first 
order by -col desc -- sorts by +col asc, implies nulls last 

しかし、これは日付では不可能である(またはそのことについては、文字列)、あなたが最初に並べ替えnullである必要がありますので、/あなたの列だけで、その後nullでなく、:[日付とヌルBY SQL ServerのORDER最後]の

order by case when col is null then 1 else 2 end, col asc|desc -- i.e. nulls first 
order by case when col is null then 2 else 1 end, col asc|desc -- i.e. nulls last 
+0

+1の対TSQLの違いを呼び出すために+1。 SQL ServerはNull値を可能な限り低い値として扱うため、 'order by col asc'が最初にNULLを返し、' order by col desc'が最後にNULLを返します。[SELECT-ORDER BY句](https://docs.microsoft.com/ en-us/sql/t-sql/queries/select-order-by-clause-transact-sql) – HappyTown

関連する問題