Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Order by numbers in proper sequence Expand / Collapse
Author
Message
Posted Wednesday, December 17, 2008 11:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 29, 2011 11:11 AM
Points: 18, Visits: 35
I have a query where the datatype I'm ordering by is varchar(10) but when I use the ORDER BY the values come out like this: How do I work around this? Thanks!

100
10000
10060
10077
10077
101
Post #621486
Posted Wednesday, December 17, 2008 11:25 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:08 PM
Points: 21,589, Visits: 27,391
If you are sure that the values are all numeric, you can do this:
ORDER BY
CAST(YourColumn as INT)




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #621497
Posted Wednesday, December 17, 2008 11:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 29, 2011 11:11 AM
Points: 18, Visits: 35
The field is alpha numeric. When I tried your solution here is the error message I received:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'R00190' to data type int.

Is there another datatype I can convert it to that will work? Thanks
Post #621501
Posted Wednesday, December 17, 2008 11:39 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:08 PM
Points: 21,589, Visits: 27,391
Okay, I did say if you knew if your data was ALL numeric. You will need to analysis your data first and see what you have. Do you have any data that looks like this 0R00012?

Once you know your data, you can look at possibile solutions.

Based on what you have provided so far, I'm not willing to throw out another suggestion yet. I'll wait until you can tell me more about your data first.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #621510
Posted Wednesday, December 17, 2008 3:04 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:01 PM
Points: 4,540, Visits: 8,184
dejanette.gordon (12/17/2008)
I have a query where the datatype I'm ordering by is varchar(10) but when I use the ORDER BY the values come out like this: How do I work around this? Thanks!

There is a lot of solutions.
But it's not clear what to solve.

Can you ask a PROPER QUESTION first?
What is the order of values you want to return?

Lynn gave you perfect answer on your question.
If it does not work then your question was incorrect.
Please fix it.
Post #621655
Posted Wednesday, December 17, 2008 3:38 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:30 PM
Points: 32,893, Visits: 26,770
Dejanette,

It would be a huge help to us if you'd post the actual data you're trying to sort in a readily consumable format along with a table creation statement. Please see the link in my signature for how to do that. Thanks.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #621678
Posted Wednesday, December 17, 2008 4:05 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:08 PM
Points: 21,589, Visits: 27,391
Well, not knowing what you are working with, and deciding to take a swag at it, try out the following code and see if that gets you going in the right direction. I would recommend reading the article that Jeff suggested (and you will also find a link to below in my signature block as well). The guidelines it provides for posting questions will greatly benefit you and us when you have other questions.

We will also be able to test our code which further benefits you as well.

create table #MyTest (
TestID varchar(10) not null,
TestData varchar(100) not null
);

insert into #MyTest (
TestID,
TestData
)
select 'R000234','Some data' union all
select '100','Some more data' union all
select '101','Some more data again' union all
select '1000','And more data' union all
select 'U1023','and even more data';

select
*
from
#MyTest
order by
TestID;

select
*,
case when left(TestID,1) like '%[A-Za-z]%'
then left(TestID,1) + right('0000000000' + substring(TestID,2,len(TestID) - 1), 9)
else right('0000000000' + TestID, 10)
end
from
#MyTest
order by
case when left(TestID,1) like '%[A-Za-z]%'
then left(TestID,1) + right('0000000000' + substring(TestID,2,len(TestID) - 1), 9)
else right('0000000000' + TestID, 10)
end;

drop table #MyTest;




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #621691
Posted Wednesday, December 17, 2008 4:39 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 29, 2011 11:11 AM
Points: 18, Visits: 35
Thanks for the replies!! I do apologize for not posting my question in the proper format. It's my first time posting, although I'm not looking for a pass based on my own ignorance of how things work around here. Here is my attempt at asking the question properly:

How do you sort alphanumeric data in sequential order? I need the data to go from:
100
101
102
etc...

instead of what is posted in the original post. In the meantime, I will read through the link on how to post properly, however I would appreciate if anyone would be willing to help me figure out this problem if I have communicated the issue properly. Much Appreciated! DG
Post #621711
Posted Wednesday, December 17, 2008 4:54 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:08 PM
Points: 21,589, Visits: 27,391
That is essentially the question you asked originally. The problem was that the information you showed did not match reality. You showed values that were all numeric. If that is the case, then what I gave you originally will work.

The problem turned out that you have non-numeric data in the alphanumeric field, so my solution failed. To determine how to sort your data, you have to understand the data. This means knowing the range of values contained in the field. Obviously, it isn't purely numeric data stored as characters. If the data is mostly numeric, with non-numeric values such as R1000 (a single leading character), then the solution I just posted would be a viable alternative.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #621719
Posted Wednesday, December 17, 2008 8:20 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:01 PM
Points: 4,540, Visits: 8,184
dejanette.gordon (12/17/2008)


How do you sort alphanumeric data in sequential order? I need the data to go from:
100
101
102
etc...

The answer is:

ORDER BY AlphanumericValue
Post #621750
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse