Search code examples
sqlms-accessms-access-2016

How to sort a column in ascending order with NULL values at the end?


I'm working on a personal database to track my projects.

I have a date/time column called StartDate in the table that I'm sorting in ascending order in the query that's used as the data source for the form where I'm entering data.

The form is a split form that displays the data in the "Detail" section in table format.

Until now, I have entered projects as they are assigned, but I have a need now to also track common projects that will be assigned but have not been assigned yet.

To track this, I wanted to enter the future projects without a start date.

However, when I enter the records like this, they are getting sorted to the top instead of at the bottom of the list where I prefer it.

Is there any way to override the sort so the records with a value in the StartDate column with sort ascending, but the records without a value in the StartDate column will sort at the end?

Thanks!


Solution

  • If you calculate anything on the date field, you'll loose the use of the index on that.

    So check for Null:

    SELECT 
        startdate, id
    FROM 
        YourTable
    ORDER BY
        StartDate Is Null,
        StartDate