Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
How to order the varchar fiels as following output

eg I Have ACCOUNT NO values like

FD2Y-01-10
NS-01-1
DDL-01-3
NS-01-10
NS-01-5
FD2Y-01-1
FD2Y-01-5
NS-01-2
DDL-01-13
DDL-01-1

SELECT ACCOUNTNO FROM MAMACCOUNTOPENING ORDER BY ACCOUNTNO
and the order values should be like

DDL-01-1
DDL-01-3
DDL-01-13
FD2Y-01-1
FD2Y-01-5
FD2Y-01-10
NS-01-1
NS-01-2
NS-01-5
NS-01-10
Posted
Updated 23-Jul-14 19:44pm
v2
Comments
Magic Wonder 24-Jul-14 1:39am    
What you have tried?
Magic Wonder 24-Jul-14 2:25am    
Is your query not working? What output you are getting?
[no name] 24-Jul-14 2:28am    
I am Getting the Output as
DDL-01-1
DDL-01-13
DDL-01-3
FD2Y-01-1
FD2Y-01-10
FD2Y-01-5
NS-01-1
NS-01-10
NS-01-2
NS-01-5

but i need as

DDL-01-1
DDL-01-3
DDL-01-13
FD2Y-01-1
FD2Y-01-5
FD2Y-01-10
NS-01-1
NS-01-2
NS-01-5
NS-01-10

I tested this and this is working fine. Here test is my table having one column named name with values you mentioned.
SQL
select name from test
order by substring(name, 1,(len(name)-charindex('-', name))-1), name


Hope this works well with you. :)
 
Share this answer
 
I think you can split the column on the basis of "-" and convert it in to three columns and put it after order by clause

for spliting columns

SQL
select LEFT(name, CHARINDEX(' ', name + '-') -1),
       STUFF(name, 1, Len(Name) +1- CHARINDEX('-',Reverse(name)), '')
from somenames




above query not for your output but this is the example for you .....
 
Share this answer
 
As in your "Question" the VARCHAR values starts with an alphabet, So a normal "ORDER BY Column_name" clause can be used to get the required result.

If you have any problem with the same take a screen shot or write the full Query with the description of database structure.

ALSO: Go to the LINK to Know more about VARCHAR.
http://msdn.microsoft.com/en-us/library/ms176089.aspx[^]

HAPPY CODING....

MY BLOG ADDRESS: http://basetutorial.blogspot.in/[^]
 
Share this answer
 
Comments
[no name] 24-Jul-14 2:34am    
WHEN RUNNING THE SCRIPT
SELECT ACCOUNTNO FROM MAMACCOUNTOPENING ORDER BY ACCOUNTNO

I am Getting the Output as
DDL-01-1
DDL-01-13
DDL-01-3
FD2Y-01-1
FD2Y-01-10
FD2Y-01-5
NS-01-1
NS-01-10
NS-01-2
NS-01-5

but i need as

DDL-01-1
DDL-01-3
DDL-01-13
FD2Y-01-1
FD2Y-01-5
FD2Y-01-10
NS-01-1
NS-01-2
NS-01-5
NS-01-10
sumitjoshi.mail 25-Jul-14 7:02am    
Gopal Please refer the MSDN Link that i have mentioned in my answer.
Try this.....
SQL
with a as
(SELECT N'FD2Y-01-10' as col UNION
SELECT 'NS-01-1' UNION
SELECT 'DDL-01-3' UNION
SELECT 'NS-01-10'UNION
SELECT 'NS-01-5' UNION
SELECT 'FD2Y-01-1' UNION
SELECT 'FD2Y-01-5' UNION
SELECT 'NS-01-2' UNION
SELECT 'DDL-01-13' UNION
SELECT 'DDL-01-1'
)
SELECT col FROM a
order BY left(col, charindex('-', col)-1), convert(FLOAT, replace(replace(col,left(col, charindex('-', col)),''),'-','.'))

Happy Coding!
:)
 
Share this answer
 
Hi,

Try this..


SQL
SELECT * FROM YOUR_TABLE ORDER BY YOUR_COL


There is nothing special required simple order by clause will work.



Hope this will help you.


Cheers
 
Share this answer
 
Comments
[no name] 24-Jul-14 2:33am    
I am Getting the Output as
DDL-01-1
DDL-01-13
DDL-01-3
FD2Y-01-1
FD2Y-01-10
FD2Y-01-5
NS-01-1
NS-01-10
NS-01-2
NS-01-5

but i need as

DDL-01-1
DDL-01-3
DDL-01-13
FD2Y-01-1
FD2Y-01-5
FD2Y-01-10
NS-01-1
NS-01-2
NS-01-5
NS-01-10

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