sorting with null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • allroad02
    New Member
    • Jan 2008
    • 1

    sorting with null

    I have three fields firstname,lastn ame,company

    I need to be able to sort by company,lastnam e , but I dont want all the NULL values displayed first. This is what I would like to do. Can I accomplish this in my SELECT?

    lastname~ company
    Anicker~Advance d Micro
    Beck~ADR Services
    Abbot~NULL
    Abid~NULL
    Stephens~Academ y of art
    Sbarge~Adams Co
    Anchor~~NULL


    Thanks,Adrian
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Originally posted by allroad02
    I have three fields firstname,lastn ame,company

    I need to be able to sort by company,lastnam e , but I dont want all the NULL values displayed first. This is what I would like to do. Can I accomplish this in my SELECT?

    lastname~ company
    Anicker~Advance d Micro
    Beck~ADR Services
    Abbot~NULL
    Abid~NULL
    Stephens~Academ y of art
    Sbarge~Adams Co
    Anchor~~NULL


    Thanks,Adrian
    the null values always come on the top, if you dont want to display null vaues on the top wat we can do is display empty string if the last name is null

    select col1,col2,isnul l(lastname,'') as lastname from tablename order by lastname

    thanks

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      here's a long one...

      select lastname, company, '1' as sortorder from yourtable where company is not null
      union all
      select lastname, company, '2' as sortorder from yourtable where company is null
      order by 3, 1, 2

      -- ck

      Comment

      Working...