473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sorting with NULL

I used SET CONCAT_NULL_YIELDS_NULL OFF do ignore NULL in concatation. I
have two fields:
chapterauthor
authors

Here's my query:
SELECT ([chapterauthor] + [authors]) AS CT FROM Items ORDER BY CT
Both Authors and ChapterAuthor can be null. I am trying to figure out if
I can ignore the null value when ChapterAuthor is null.

Sample data/result:
Appadurai, Arjun #chapter author is null
Appadurai, ArjunFardon, Richard (ed.) # (chapterauthor+authors)

I would like the output to be like this:
Appadurai, ArjunFardon, Richard (ed.) # (chapterauthor+authors)
Appadurai, Arjun #chapter author is null

I guess I can write a stored procedure with if/else, but I was wondering
If I can do this with a simple query. Thanks.
Jul 28 '06 #1
1 1905
Sharif Islam (mi****@spam.uiuc.edu) writes:
I used SET CONCAT_NULL_YIELDS_NULL OFF do ignore NULL in concatation.
That setting is only for legacy software and there is no reason to use
it new code.
Here's my query:
SELECT ([chapterauthor] + [authors]) AS CT FROM Items ORDER BY CT
SELECT coalesce(chapterauthor, '') + coalesce(authors, '') AS CT
FROM Items

coalesce() is a function that accept a list of values as parameters and
returns the first non-NULL value in the list.
Both Authors and ChapterAuthor can be null. I am trying to figure out if
I can ignore the null value when ChapterAuthor is null.

Sample data/result:
Appadurai, Arjun #chapter author is null
Appadurai, ArjunFardon, Richard (ed.) # (chapterauthor+authors)

I would like the output to be like this:
Appadurai, ArjunFardon, Richard (ed.) # (chapterauthor+authors)
Appadurai, Arjun #chapter author is null

I guess I can write a stored procedure with if/else, but I was wondering
If I can do this with a simple query. Thanks.
I am not sure that I understand exactly what you are looking for, but
try:

ORDER BY CASE WHEN chapterauthor IS NOT NULL THEN 1 ELSE 0 END, CT
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
4
by: Roy | last post by:
Greetings, I've been avoiding it for so long, but like an evil wraith it always returns to haunt me. The bane of my existence, it is... bidirectional sorting!!! Checked out previous posts and...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: iKiLL | last post by:
Hi All, My background is that i am new to C- Sharp and OOP. I have been working with it for about 2 Weeks. I do have good experionce in developing in things like VB6, COM, ASP, JavaScript and i...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
7
by: abracadabra | last post by:
I am reading an old book - Programming Pearls 2nd edition recently. It says, "Even though the general C++ program uses 50 times the memory and CPU time of the specialized C program, it requires...
5
by: saytri | last post by:
I've got a problem in sorting. I have to use a bubble sort to sort the scores with those who bring the highest percent at the top. The scores of each user i saved in a text file. This is how i...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.