Monday, December 29, 2003

Query to display Null values at the bottom ...

Let us assume that a table has following records in it:

Sno, FirstName
1, NULL
2, 'Vadivel'
3, 'Sachin'
4, NULL

If we write a select statement as follows

Select * from sampleTable
Order by FirstName


The result would be:

Sno, FirstName
1, NULL
4, NULL
3, 'Sachin'
2, 'Vadivel'

If you want to push all the NULL values to the bottom of the result then use the below the query.

Select * from sampleTable
Order by
 Case
   When FirstName Is Null Then 1
 Else 0
 End,
FirstName

No comments: