Solved

Sorting string field in Natural order

Posted on 2004-10-08
2
829 Views
Last Modified: 2008-02-07
Hello,

I have a table which is have a field "file_ref". This is a string field. It has value like

1/1
1/2
1/3
1/4
1/5
1/6
1/7
1/8
1/9
1/10
1/11 ..... so on

when is use the query order by file_ref. it shows me something like

1/1
1/10
1/11
1/2
1/3
1/4
1/5
1/6
1/7
1/8
1/9 ..... so on

can anyone help me in sorting this data.
0
Comment
Question by:majidbhatti
2 Comments
 
LVL 9

Accepted Solution

by:
paelo earned 30 total points
ID: 12265241
Assuming the format is standardized (integer followed by forward slash followed by integer), try:

SELECT y.*
FROM yourtable y
ORDER BY CONVERT(int,LEFT(y.[file_ref],CHARINDEX('/',y.[file_ref])-1)), CONVERT(int,RIGHT(y.[file_ref],LEN(y.[file_ref])-CHARINDEX('/',y.[file_ref])))

-Paul.
0
 
LVL 3

Assisted Solution

by:SQLMaster
SQLMaster earned 20 total points
ID: 12265862
Or Use this:

select First_Split + '/' + Second_Split as file_ref from (select substring(file_ref,1,charindex(file_ref,'/')-1) as First_Split, substring(file_ref,charindex(file_ref,'/')+1) as second_split) tmp_table
order by First_Split, Second_Split

Dheeraj
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now