?
Solved

Sorting string field in Natural order

Posted on 2004-10-08
2
Medium Priority
?
852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
paelo earned 90 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 60 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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…

650 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