Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Advanced Order By Logic

Author  Topic 

cominginsecond
Starting Member

2 Posts

Posted - 2007-02-20 : 22:42:31
I apologize if this is not the appropriate forum for this question.

I've written a searchable database-driven application in classic ASP and vbscript with a SQL Server backend. What I need to do is this: order the results of a query so that if the first "order by" field is null to order that entry based on the second "order by" field.

The application I am writing is a database of books, and my client wants the results to be ordered by Author, unless there is no Author, in which case he wants that entry ordered by book title. Here is an example of how he wants the books sorted:

Adamson, Jan - Book X
Bible, The
Wilson, Jonathan - Book Y

I hope I've explained the scenario correctly. What's the best way to write a SQL statement that will yield the ordering criteria described above? Also, is there any way to get "Order by" to ignore articles like "A" and "The?"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-20 : 22:47:00
quote:
What I need to do is this: order the results of a query so that if the first "order by" field is null to order that entry based on the second "order by" field.

like this ?

order by coalesce(Author, Title), Title


quote:

Also, is there any way to get "Order by" to ignore articles like "A" and "The?"


try this, use replace() to remove the 'The ' and 'A '

order by replace(replace(Title, 'The ', ''), 'A ', '')



KH

Go to Top of Page

cominginsecond
Starting Member

2 Posts

Posted - 2007-02-21 : 13:49:23
Thank you! I'll give these a shot.
Go to Top of Page
   

- Advertisement -