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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Numerical Order By

Author  Topic 

sconard
Starting Member

18 Posts

Posted - 2005-08-24 : 15:57:30
I have a column "title" that is nvarchar 255. I would like ORDER BY results to be standard alpha-numeric. Query of table returns ORDER BY Titles as

Lesson 1
Lesson 10
Lesson 2

Instead of...

Lesson 1
Lesson 2
Lesson 10

Must I convert the datatype prior to sort?
How is this done?
Thank you.

SamC
White Water Yakist

3467 Posts

Posted - 2005-08-24 : 16:43:39
Posting the query you used to get those results would help a lot. Is that a single column or two columns of data. Was an ORDER BY used? What columns were ordered?

I'm guessing your column "title" has two tokens: "Lesson" separated with a space from "NN" where NN ranges from 1 to 99.

Conversion can be done right on the ORDER BY

SELECT MyTltle
FROM MyTable
ORDER BY CAST(RIGHT(MyTitle, 2) AS INT)
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-08-24 : 16:49:34
Ideally you would have seperate columns for lessonNumber and lessionTitle.
You can sort by one column and display the other. But if you're stuck with this
structure then you'll need to do something like Sam provided.

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-25 : 02:15:00
Try this also

Select title from yourTable order by len(title),title

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -