Question : Sorting string field in Natural order

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.

Answer : Sorting string field in Natural order

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.
Random Solutions  
 
programming4us programming4us