Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting with nulls

Status
Not open for further replies.

WorkSuxs

Programmer
Jan 26, 2005
28
US
My problem is I need to order by a column that has nulls in, present the records with nulls last AND the ones with data needs to be sorted ascending like below:

specialNumber name
1 paul
10 jill
44 mark
null jane
null bob
 
could do ...

Code:
select specialnumber,name
from table
order by isnull(specialnumber,9999999999) asc
 
need to shorten the 9999999999 to 999999999 to stop arithmetic overflow if specialnumber is int

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
I tried both and I'm still getting the nulls first and then the numbers is ascending order. Maybe I'm doing something wrong. Here is the sql I'm sending:

select * from tableName where servtype='Bike' and (ReqDate between '2004-01-01' and '2005-01-01') and Division ='West' order by isnull(PromoCode,'99999999') asc
 
Thanks

Yes, I did and the same problem nulls first then numbers in ascending order.

I have to leave for the day. Please post anything else and I'll try it first thing in the morning.

Thanks for the info (both of you)
 
Odd I know my code worked on my test table. I tried it again on a different table and it worked.

Maybe some other part of the query is affecting this? Could you post the whole query that you used?

Questions about posting. See faq183-874
Click here to learn Ways to help with Tsunami Relief
 
OK, I'm at home. I don't have access to my code, but I had posted my entire sql statement above. For your's (SQLSister) I just exchanged everything after "order by".
 
I think we may have been through this one before. Is there a char(0) at the beginning of your PromoCode field? I see that it's really a varchar column, isn't it?
If there is some sort of unprintable character then put the same unprintable character as a prefix in the 2nd parameter of the IsNull() function.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Very sorry, what is char(0)? the column is a var with a length of 7. I tried changing the "order by isnull(PromoCode,'9999999') asc" to only 7 in the '9999999'.
 
OK, I just got it...I'm really embarassed to say, but I was too deep into the problem to remember that NULL and blank are two different things. I put in NULL and it worked just fine.

Thanks everyone who responded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top