Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Ordering nulls first, then numbers starting at 0

I have a query that brings back a field that can be nulls or numbers. If I ORDER BY using DESC on this field, I get the nulls at the beginning, but then the numbers start at 999 and work their way down to 000 after the nulls. Is there a way to get the null rows returned first, and then have the numbers start at 000 - 999? Here is an example using three rows. Row 1 is the row I want to have nulls first then numbers after the nulls 000-999. DESC works like this today:

``` 1    2    3
|XXXX|ABC
|XXXX|XYZ
999|XXXX|XYZ
001|XXXX|ABC
000|XXXX|ABC
```
I would like it to be:
``` 1    2    3
|XXXX|ABC
|XXXX|XYZ
000|XXXX|ABC
001|XXXX|ABC
999|XXXX|XYZ
```
Is this possible?

Sure, it is possible, you just have to use a little "magic" to get things ordered the way you want to. Your problem lies in the fact that the NULL value lies after all numbers when doing a sort. According to SQL standards, NULL has to be the lowest or the highest value in a sort. It can't be in the middle. And Oracle chooses to implement it as the highest value in the sort. You'd like it to be the opposite. So let's change that!

First, you have to know the range of possible values in your data. I'm going to assume that the range of possible values is 0-999. I've set up a test table with some dummy data as you can see below:

```ORA9I SQL> select * from test;

ID
----------
5

993
104
```
The second row in the table has a NULL value. Now, if I want this NULL value to appear first in my sorted list, I have to assign a value lower than the minimum in my range in the ORDER BY clause. Since the range is 0-999, any negative number will work. I'll use the NVL function to replace NULL values with a negative number. The following query does what you are seeking:
```ORA9I SQL> SELECT * FROM test ORDER BY NVL(id,-1);

ID
----------

5
104
993
```
As you can see, I substituted a negative number for any NULL values, only in the ORDER BY clause. I did not perform this substitution in the SELECT clause, so it doesn't show up in my result set. And the NULL column appears first in the list, when it normally appears last by default.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Swim DataFabric platform helps to understand edge streaming data

Swim released its new Swim DataFabric, which integrates with Microsoft Azure to help users organize and gain insights from ...

• ### Dremio Data Lake Engine 4.0 accelerates query performance

Dremio issues a new platform update, defining itself as data lake engine technology that looks to help users connect and query ...

• ### Weighing the use of third-party database administration tools

Database expert Chris Foot details the key reasons why DBAs should consider using third-party database administration to fill ...

• ### Qlik exec discusses AI and its role in the future of BI

The next major trend in business intelligence will be the increasing impact of augmented intelligence and machine learning, ...

• ### SAP BI platform stays strong due to cloud-based architecture

A cloud-native BI platform along with domain-specific applications that can be embedded to serve the needs of various industries ...

• ### Tableau 2019.3 highlighted by enhanced AI, new data catalog

Tableau released its third-quarter update Wednesday, featuring Explain Data, an enhanced augmented intelligence tool, and Tableau...

## SearchSAP

• ### Navy sails SAP ERP systems to AWS GovCloud

The Navy consolidated 26 various ERP systems onto SAP HANA on the AWS GovCloud, leading to data reduction, lower TCO, and setting...

• ### SAP HANA application collects concussion data for real-time analysis

Protecht, an SAP HANA based system, enables teams in contact sports to measure and analyze data about hits though an IoT sensor ...

• ### S/4HANA public cloud vs. private cloud: Compare the pros, cons

In addition to SAP's on-premises and hybrid models, the vendor offers S/4HANA in public and private clouds. Here's a look at the ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Microsoft vs. IBM: A major shift in Java support

Once an afterthought in the Java community, Microsoft has seemingly overtaken IBM as the preeminent advocate among developers at ...

• ### Guide to practicing cloud-native development

In this guide, learn about cloud-native development -- what it is, how vendors try to exploit it and how to bring it to life with...

• ### Eclipse launches Che 7 IDE for Kubernetes development

The Eclipse Foundation has introduced Eclipse Che 7, a new version of the Eclipse Che cloud-based integrated development ...

## SearchDataCenter

• ### New Dell EPYC servers embrace AMD Rome chips

Dell EMC goes from Naples to Rome with a new line of EPYC servers, including Ready Solutions for high-performance computing and ...

• ### Can next-gen SIEM help cybersecurity initiatives?

More organizations are using SIEM, AI and cloud technology to minimize security breaches. Though despite interest, this ...

• ### IBM z15 mainframe secures data across multi-cloud environments

IBM unveiled the latest in its line of mainframes capable of processing 1 trillion web transactions a day. The IBM z15 ...

## SearchContentManagement

• ### Microsoft 365 Business update targets nonprofits

Microsoft announced this month that it is releasing new updates and offerings for nonprofits, addressing concerns customers had ...

• ### Nintex launches no-code workflow automation tool

Nintex has launched a no-code workflow automation tool, Workflow Generator, in an effort to bridge the gap between IT departments...

• ### Cohesion is latest Acquia acquisition

While Acquia would not provide specifics of the deal, the decision behind the acquisition of Cohesion was the chance to bring ...

## SearchHRSoftware

• ### 10 ingredients in a killer employee experience strategy

Improving the employee experience is a must for modern organizations and their HR teams, but where should you start? Here are 10 ...

• ### SuccessFactors customers to see big Qualtrics impact

SuccessFactors is rethinking its HCM suite as it works to integrate Qualtrics into its applications. But HR managers are ...

• ### HiQ Labs vs LinkedIn case OKs robot monitoring of employees

A U.S. Court of Appeals ruled on the LinkedIn vs HiQ lawsuit, which could help shape how services gather information on social ...

Close