Nulls First In SQL Server

Today someone ask about how to order a column, she want order nulls at the beginning of the report, she told that can perform it on Oracle. She is correct, or maybe not, many people think NULLS FIRST or NULLS LAST is an impletantion by Oracle but it isn’t true. This syntax is defined by the standard, ISO 9075/ANSI SQL. Many database vendors not implement this feature

In this case the query is on SQL Server, a simple way to resolve it is like show below;

 
SELECT 
S.SESSION_ID,
WT.WAIT_TYPE
FROM SYS.DM_OS_WAITING_TASKS AS WT
RIGHT OUTER JOIN SYS.DM_EXEC_SESSIONS AS S ON WT.SESSION_ID = S.SESSION_ID
WHERE S.IS_USER_PROCESS = 0
ORDER BY ISNULL (WT.WAIT_TYPE, '1')

With this query you get the nulls first, and change the 1 by ZZZ (for example) you get nulls last.

HTH – Antonio NAVARRO

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s