1

I am currently try to sort date in ASC. If i used ASC it will sort the data will null date than the next date.

So i do something like this Select * from tblUser order by LastLogin and it will display the following

Name             LastLogin 
1. Peter         NULL
2. Brian         2013-03-14
3. Jack          2013-03-15

But i want to return something this and please advise i can achieve this

1. Brian         2013-03-14
2. Jack          2013-03-15
3. Peter         NULL
John Woo
  • 258,903
  • 69
  • 498
  • 492
Spidey
  • 1,583
  • 5
  • 23
  • 30

1 Answers1

2
SELECT  *
FROM    tableName
ORDER   BY CASE WHEN lastlogin IS NULL THEN 1 ELSE 0 END, lastlogin
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    That works; Also if you are on SQL Server 2012 you can use: `ORDER BY LastLogin ASC NULLS LAST` – Gibron Mar 26 '13 at 07:19
  • Ah, I figured; We don't use 2012 yet... I've been itching to use that so it was ready and loaded ;) – Gibron Mar 26 '13 at 07:23