Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:58:32+00:00 2026-05-15T14:58:32+00:00

So here’s what the table looks like ——– | field1 | |========| | 1.2

  • 0

So here’s what the table looks like

    --------
   | field1 |
   |========|
   |  1.2   |
   |  1.7   |
   |   -    |
   |   -    |
   |  1.3   |
  • the dashes are actually blank fields

My current SQL statement does a simple “ORDER BY field1” which puts the blank ones on top, however I want to get the numbers on top, and put the empty ones at the bottom.

EDIT:

My order by code looks like this:

   'Get the sort requirement as per the requirement
   strSql = strSql & " ORDER BY [RequestType], [Rank], [ADRNo]"

So i want it to order by requesttype, rank, and then ADRNo. But i want the null values to come last for all three fields. So I posted one field in my orignal question, but I am really working with 3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:58:32+00:00Added an answer on May 15, 2026 at 2:58 pm
    ...order by iif(field1 is null, 1, 0), field1
    

    Logic

    The iif(field1 is null, 1, 0) constructs a virtual column with 1 for all null values. so you have

        -------- -------
       | field1 |       |
       |========|=======| 
       |  1.2   |   0   |
       |  1.7   |   0   |
       |   -    |   1   |
       |   -    |   1   |
       |  1.3   |   0   |
    

    and then the sort of this virtual column takes care of putting the numbers below the nulls.

    Multiple columns

    If you have multiple columns that could have nulls you’d need an additional calculated column for each column. Something like

        -------- ------- -------- -------
       | field1 |   s1  | field2 |   s2  |
       |========|=======|========|=======| 
       |   2    |   0   |   -    |   1   |
       |   1    |   0   |   1    |   0   |
       |   -    |   1   |   3    |   0   |
       |   -    |   1   |   -    |   1   |
       |   3    |   0   |   2    |   0   |
        -------- ------- -------- -------
    

    now you have 2 options.

    1. you can sort each field with the null at the end. so

    ...order by iif(field1 is null, 1, 0), field1, iif(field2 is null, 1, 0), field2

    would give you

        -------- ------- -------- -------
       | field1 | field2 |   s1  |   s2  |
       |========|========|=======|=======|
       |   1    |   1    |   0   |   0   |
       |   2    |   -    |   0   |   1   |
       |   3    |   2    |   0   |   0   |
       |   -    |   3    |   1   |   0   |
       |   -    |   -    |   1   |   1   |
        -------- ------- -------- -------
    

    note that in field2 there is a null (row 2) before 3 because the sort for field1 (2) overrides the null sort logic (s2) for field2. the second null (row 5) in field2 comes after 3 (row 5) because of our null specific sort logic for s2.

    2. you can choose to have all nulls in field2 at the end, before sorting on field1. your code would then be…

    ...order by iif(field1 is null, 1, 0), iif(field2 is null, 1, 0), field1, field2

    which gives you

        -------- ------- -------- -------
       | field1 | field2 |   s1  |   s2  |
       |========|========|=======|=======|
       |   1    |   1    |   0   |   0   |
       |   3    |   2    |   0   |   0   |
       |   2    |   -    |   0   |   1   |
       |   -    |   3    |   1   |   0   |
       |   -    |   -    |   1   |   1   |
        -------- ------- -------- -------
    

    Note

    (Ref: Tom Gullen’s comment in question) This takes care of putting only nulls at the end (empty strings, or strings with spaces will still be in the natural sort order)

    Options

    So.. in your case you’d be using either

    ...ORDER BY 
                IIF([RequestType] IS NULL, 1, 0), [RequestType], 
                IIF([Rank] IS NULL, 1, 0), [Rank], 
                IIF([ADRNo] IS NULL, 1, 0), [ADRNo]
    

    or

    ...ORDER BY 
                IIF([RequestType] IS NULL, 1, 0), 
                IIF([Rank] IS NULL, 1, 0), 
                IIF([ADRNo] IS NULL, 1, 0), 
                [RequestType], 
                [Rank], 
                [ADRNo]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting an error here that says I haven't defined a method, but it
This is beyond both making sense and my control. That being said here is
I would like to remove/delete a migration file. How would I go about doing
IE is giving me an undefined NAN when i try to view the calender...
I am writing a query to fetch results for all the values in a
Let say I have the following desire, to simplify the IConvertible's to allow me
There doesn't seem to be any tried and true set of best practices to
I have found this example on StackOverflow: var people = new List<Person> { new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.