Unix Technical Forum

SQL order question

This is a discussion on SQL order question within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi- I've got some issues with the ordering of search results. I've got 4 fields, (1, 2, 3, 4). ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:34 AM
Cindy
 
Posts: n/a
Default SQL order question

Hi-

I've got some issues with the ordering of search results. I've got 4
fields, (1, 2, 3, 4). Most often there is data in field 1. Sometimes
there isn't and so then I'd like to look at field 2.

I can accomplish most of what I want with this:

select
1 as orderField,
ISNULL(1, 2) AS orderField,
from
table
order by
orderField


Of course there are occassions when sometimes both 1 and 2 are blank
and so I want to use whatever value is in 3 or 4, but the results need
to all be in alphabetcal order.

here's some sample data:


record 1: field 1 - apple
record 2: field 1 - peach
record 3: field 2 - orange
record 4: field 3 - banana
record 5: field 4 - grape


so with the current query, the order would be:
banana, grape, apple, orange, peach

I need it to be in alphabetical order no matter which of the 4 fields
it came from:
apple, banana, grape, orange, peach


Is this possible?

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:34 AM
John Bell
 
Posts: n/a
Default Re: SQL order question

Hi

Check out the coalesce function in books online.

SELECT Col1, Col2, Col3, Col4
FROM MyTable
ORDER BY COALESCE ( Col1, Col2, Col3, Col4 )

John

"Cindy" <cschall@yahoo.com> wrote in message
news:3b69843f.0407102315.485600c5@posting.google.c om...
> Hi-
>
> I've got some issues with the ordering of search results. I've got 4
> fields, (1, 2, 3, 4). Most often there is data in field 1. Sometimes
> there isn't and so then I'd like to look at field 2.
>
> I can accomplish most of what I want with this:
>
> select
> 1 as orderField,
> ISNULL(1, 2) AS orderField,
> from
> table
> order by
> orderField
>
>
> Of course there are occassions when sometimes both 1 and 2 are blank
> and so I want to use whatever value is in 3 or 4, but the results need
> to all be in alphabetcal order.
>
> here's some sample data:
>
>
> record 1: field 1 - apple
> record 2: field 1 - peach
> record 3: field 2 - orange
> record 4: field 3 - banana
> record 5: field 4 - grape
>
>
> so with the current query, the order would be:
> banana, grape, apple, orange, peach
>
> I need it to be in alphabetical order no matter which of the 4 fields
> it came from:
> apple, banana, grape, orange, peach
>
>
> Is this possible?
>
> Thanks!



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:34 AM
David Portas
 
Posts: n/a
Default Re: SQL order question

You can use COALESCE:

.... ORDER BY COALESCE(col1, col2, col3, col4)

Be aware that the columns will all be cast to a single datatype (the
datatype of highest precedence among them) so they must all be of compatible
types. If the columns are not of the same datatype then the conversion may
affect the ordering, which may differ from what you expect.

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com