Solved

Sorting string field in Natural order

Posted on 2004-10-08
2
830 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

895 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

13 Experts available now in Live!

Get 1:1 Help Now