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

  • SEARCH
  • Home
  • 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 7675449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:57:55+00:00 2026-05-31T16:57:55+00:00

In SQL Server, I have a column such as ID containing integers in descending

  • 0

In SQL Server, I have a column such as ID containing integers in descending order with one null value
ex.

ID
====
20
19
18
...
2
1
null

select id from mytable order by id desc

I need this to be ordered descending but null on top:

ID
====
null
20
19
18
...
2
1

The null is a result of a prior

union all

and needs to be there.
I had a few ideas, such as create an artificial large number.
Just want to see what your guys can come up with.
Have fun!

  • 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-31T16:57:56+00:00Added an answer on May 31, 2026 at 4:57 pm

    Use a CASE in your ORDER BY to force the NULLs first. This is supported by SQL Server.

    SELECT
      id
    FROM mytable
    ORDER BY 
      CASE WHEN id IS NULL THEN 0 ELSE 1 END,
      id DESC
    

    It works by deriving a “column” on which to sort that holds a 0 for NULL and 1 for everything else. The 0 sorts ahead of the 1. Then, we add the rest of the ORDER BY column chain to continue sorting, so adding id DESC forces the remaining rows (those with our derived 1) to sort in descending order by id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to use conditional select query in SQL Server ? I have a column
I have a SQL Server 2000 with a table containing an image column. How
I have a legacy SQL Server 2000 database that has one column that has
In SQL Server, I have a start time column on a table such as:
I have an untyped XML column in Sql Server Database which holds values such
Does each column of a table in SQL Server have a unique id? I've
I have a Money column in my SQL Server 2008 table. In my below
I have a SQL Server 2000 database with a column of type VARCHAR(255). All
I have an SQL Server 2008 database with a column of type geography storing
I have a SQL Server 2000 database that will not display the column list

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.