How can I sort my data in alphabetic order?

Sort data in varchar alphabet order

  • I have table with following data . 1 AAAAA01 AAAAA01 AAAAA01 B21 AAAAAA1 B3 AB100 and I want to sort data in following order AAAAAA1 AAAAA01 AAAAA01 AAAAA01 AB100 B21 B3 1 I wrote a statement but not give me correct result. Select * from dbo.Section order by CASE WHEN not Section like '%[^0-9]%' THEN CONVERT(int,Section) WHEN Section like '[0-9]%' THEN CONVERT(int,SUBSTRING(Section,1,PATINDEX('%[A-Z]%',Section)-900000)) END For your help i am providing script of table INSERT [dbo].[Section] ([Section]) VALUES (N'1') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01') INSERT [dbo].[Section] ([Section]) VALUES (N'B21') INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAAA1') INSERT [dbo].[Section] ([Section]) VALUES (N'B3') INSERT [dbo].[Section] ([Section]) VALUES (N'AB100') INSERT [dbo].[Section] ([Section]) VALUES (N'2') INSERT [dbo].[Section] ([Section]) VALUES (N'B1') INSERT [dbo].[Section] ([Section]) VALUES (N'B32') INSERT [dbo].[Section] ([Section]) VALUES (N'11') INSERT [dbo].[Section] ([Section]) VALUES (N'A10') INSERT [dbo].[Section] ([Section]) VALUES (N'ABAAAA') Can you please tell me how can this provide me correct answer.

  • Answer:

    Please try: Select * from dbo.Section order by REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE ([Section], '0', 'ZZ0'), '1', 'ZZ1'), '2', 'ZZ2'), '3', 'ZZ3'), '4', 'ZZ4'), '5', 'ZZ5'), '6', 'ZZ6'), '7', 'ZZ7'), '8', 'ZZ8'), '9', 'ZZ9'), [Section] http://sqlfiddle.com/#!3/d37e1/4 Result of select for new test data: http://i.stack.imgur.com/u0cwD.png Please check is it correct to you.

A.Goutam at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

This is another way. Assuming your number strings are greater than -100000000. SELECT YourString FROM YourTable ORDER BY CASE WHEN YourString LIKE '[0-9]%' THEN CONVERT(int, YourString) ELSE -100000000 END, YourString

Kaf

I tried to split your field in two, alpha and num. In order to order the query by Alpha, first and Num, second. But AAAAA1 is after AAAA01 with my logic... Do you have an explicite rule to explain why do you want AAAA01 before AAAAA1 ? SELECT SECTION.section, CASE WHEN section NOT LIKE '%[^A-Z]' THEN section WHEN section LIKE '[A-Z]%' THEN LEFT (section, PATINDEX ('%[^A-Z]%', section)-1) ELSE 'ZZZZZZZZZZZZZ' END AS Alpha, CASE WHEN ISNUMERIC(section)=1 THEN CAST( section AS int) WHEN SECTION.section LIKE '%[0-9]' THEN CAST( RIGHT (section, PATINDEX ('%[^0-9]%', REVERSE (section))-1 ) AS INT) ELSE 0 END AS Num FROM SECTION ORDER BY Alpha, Num; http://sqlfiddle.com/#!3/a1a03/1

Fabienne B.

How about this SQL? i tried with your data and it's returned the correct order SELECT section FROM section ORDER BY LEN(LEFT(Section, PATINDEX('%[0-9]%', Section)-1)) DESC Responding to @A.Goutam's comment below (i'm sorry i don't know how to add image to the comment) I just copied the data given by @A.Goutam and tried it directly in my sql server as shown below http://i.stack.imgur.com/o9aVB.png New SQL query with new data SELECT section AS section FROM section ORDER BY LEN(section + '1') DESC , LEN(LEFT(section + '1', PATINDEX('%[0-9]%', section + '1')-1)) DESC , CONVERT(INT,SUBSTRING(section + '1',PATINDEX('%[0-9]%',section + '1'),LEN(section + '1'))) DESC Result: AAAAAA1 AAAAA01 AAAAA01 AAAAA01 AAAAA01 AAAAA01 ABAAAA AB100 B32 B21 A10 B3 B1 11 2 1

Fathir Mohamad

You can accomplish what you're trying to get with temporary table as well. However, for larger records this can add some overhead to performance. So, i suggest you to use it sparingly. CREATE table #temp ( Section varchar(20) ) INSERT INTO #temp SELECT * FROM dbo.Section WHERE section LIKE '[A-Z]%' order by section ASC INSERT INTO #temp SELECT * FROM dbo.Section WHERE section LIKE '[0-9]%' order by section ASC SELECT * FROM #temp drop table #temp

NZeta520

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.