Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi, my teacher gave me task like ....

First store this data in a database then

Sort this :
A-1
A-2
A-12
A-2-A
A-1-A-1
SHOP-1
B-42
B-1
C-240
B-1-A
B-2
MB-12
HANGAR-1

In this form

A-1
A-12
A-1-A-1
A-2
A-2-A
B-1
B-1-A
B-2
C-240
MB-12
HANGAR-1
SHOP-1

Simple Sorting... any one can plx give me some idea or clue how can i do this . which query i should use here ??

regards
Posted

1 solution

Use ORDER BY [column] ASC

SQL
CREATE TABLE [dbo].[test](
    [Id] [int] NOT NULL,
    [value] [nvarchar](max) NOT NULL
)

SELECT [Id]
      ,[value]
  FROM [database_name].[dbo].[test]
  ORDER BY value ASC
 
Share this answer
 
Comments
Nelek 15-Apr-12 6:37am    
Please next time it is clear the question is homework, point out the direction, but don't give a direct solution. If you do that, how are they going to learn?
Dhanushka Madushan lk 15-Apr-12 11:04am    
Sorry my mistake ... wont happen again..
Nelek 15-Apr-12 18:59pm    
Don't worry, it is not a crime
syed armaan hussain 15-Apr-12 12:30pm    
@DHANUSHKA

i did it with

order by [column name] ASC but it gives me result like this

A-1
A-1-A-1
A-12
A-2
A-2-A
B-1
B-1-A
B-2
B-42
C-240
HANGAR-1
MB-12
SHOP-1

but as i mentioned above i want my result like this

A-1
A-12
A-1-A-1
A-2
A-2-A
B-1
B-1-A
B-2
C-240
MB-12
HANGAR-1
SHOP-1 "there is some diffrence between these two tables"
Nelek 15-Apr-12 18:57pm    
The difference is because the value of "-" is lower than "2". That's why the ASC sorting brings it before. Take a look into the notes of the lecture, search for the SORT options and try to figure out how to mix them so that you get the long A-1-A-1 before the A-12

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