Solved

Sorting string field in Natural order

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

830 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