Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to sort the number from asscending value. sample data is

463919493       
463919493 02
463919493 02
463919493 03
463919493 01
463919493 03
463919493 01


i need to get output as
463919493       
463919493 01
463919493 01
463919493 02
463919493 02
463919493 03
463919493 03



and suppose if we have alphabet with number, how to sort this number in asscending

HO463919493       
HO463919493 02
HO463919493 02
HO463919493 03
HO463919493 01
HO463919493 03
HO463919493 01


Need to get output as:

HO463919493       
HO463919493 01
HO463919493 01
HO463919493 02
HO463919493 02
HO463919493 03
HO463919493 03


What I have tried:

463919493       
463919493 02
463919493 02
463919493 03
463919493 01
463919493 03
463919493 01


i need to get output as
463919493       
463919493 01
463919493 01
463919493 02
463919493 02
463919493 03
463919493 03



and suppose if we have alphabet with number, how to sort this number in asscending

HO463919493       
HO463919493 02
HO463919493 02
HO463919493 03
HO463919493 01
HO463919493 03
HO463919493 01



HO463919493       
HO463919493 01
HO463919493 01
HO463919493 02
HO463919493 02
HO463919493 03
HO463919493 03
Posted
Updated 26-Apr-18 7:45am
v2
Comments
Maciej Los 26-Apr-18 8:38am    
Do not post the same content! Please, provide information about your way to sort data. Describe what have failed.

If you would like to treat text data as numeric, you have to convert it. For example:
SQL
SELECT * 
FROM (
    SELECT 'HO463919493' AS F1, NULL AS F2
    UNION ALL       
    SELECT 'HO463919493', '02'
	UNION ALL       
    SELECT 'HO463919493', '02'
	UNION ALL       
    SELECT 'HO463919493', '03'
	UNION ALL       
    SELECT 'HO463919493', '01'
	UNION ALL       
    SELECT 'HO463919493', '03'
	UNION ALL       
    SELECT 'HO463919493', '01'
) AS T
ORDER BY CONVERT(INT, SUBSTRING(F1, 3, LEN(F1))) ASC, CONVERT(INT, F2) ASC


Note: Above sample is for specific data, where first two letters are rejected during conversion.
 
Share this answer
 
v3
Comments
Wendelius 26-Apr-18 11:45am    
That would do it.
To add to both solutions: Why do you store the two separate values in a single column in the first place? Your SQL operations would be much easier if you would have the values both in separate columns. Then you could simply order the by using an ORDER BY clause like
SQL
ORDER BY Col2 ASC, Col1 ASC
 
Share this answer
 
You have not shown us any SQL query.

Usually you have to append
SQL
ORDER BY [columnname] ASC
to your query where columnname is the name of your second column. Because ASC is the default sort order it can be omitted.

You can also specify multiple columns or more complex expressions. See ORDER BY Clause (Transact-SQL) | Microsoft Docs[^] for more information.
 
Share this answer
 
Comments
Maciej Los 26-Apr-18 8:56am    
OP wants to trat text data as a numeric. See my answer.
Jochen Arndt 26-Apr-18 9:05am    
Ok. But with his example data a simple ORDER BY on the second column should do it. The first column may be added to the expression for sub-sorting. But in his examples that column has unique data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900