Tuesday, July 16, 2013

Sorting Columns With Null Values In SOQL

Recently one of my friend was faced a problem. He wants contact list in sorted order, but the issue is he wants sorting the table according to email columns where some records are without email value.
In that case sorting result is like this type
he fired this Query -
select id, lastname, email from contact order by email 

When he tried to sort in descending order then result is -
he fired this Query -
select id, lastname, email from contact order by email DESC

In a descending case result was wrong. Result must be show where null email values are at the last.
Sorting are working on filled values not on Null value.
So,
I found the way of sorting values including null values. -

select id, lastname, email from contact order by email DESC NULLS LAST

Using this query you will get this type of result - 

NULLS LAST is a keyword for including the null values in a order column. 




6 comments:

  1. Great one , some images are not visible specially 1 and 2nd one ..
    I do also own one website named www.salesforcegeek.com and please suggest

    ReplyDelete
    Replies
    1. Hi,

      I am not getting you Perfectly, I think here you taking about Images that I have used in this blog (Screenshot). If I am wrong, please correct me.

      You can open these image via below URL -

      Image 1 : http://3.bp.blogspot.com/-YPusMrcj7ic/UeVWxyapz6I/AAAAAAAAAVg/jkhr3eVkkl4/s1600/sorting+1.JPG

      Image 2 : http://3.bp.blogspot.com/-HBej8OuTrTo/UeVXSOOPDhI/AAAAAAAAAVo/z3wDOJuxO3A/s1600/sorting+2.JPG

      Thanks

      Delete