Click here to Skip to main content
15,077,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Name Sort By Numbers, special characters, Alphabetical order


Name starts with Numbers
Name starts with special characters
Name starts in Alphabetical order


default the Name details from ascending to descending order based on the "Name" Field

If the Name starts with Numbers or special characters, display those Names on the
top of the list. Where it needs to sort it in the following order.
Name starts with Numbers
Name starts with special characters
Name starts in Alphabetical order


Descending order
  Name starts in Alphabetical order
  Name starts with special characters
  Name starts with Numbers


The Solution which i have tired was not working . Can any one please provide the solution.

What I have tried:

Declare  @Sorting TABLE(BusinessName varchar(50) NULL)
Declare @sortOrder  varchar(10) = 'ASC'  -- DESC

INSERT INTO @Sorting (BusinessName) VALUES ('ABCD')
INSERT INTO @Sorting (BusinessName) VALUES ('Zeebra')
INSERT INTO @Sorting (BusinessName) VALUES ('& ABCD')
INSERT INTO @Sorting (BusinessName) VALUES ('2 DEF')
INSERT INTO @Sorting (BusinessName) VALUES ('Hello &')
INSERT INTO @Sorting (BusinessName) VALUES ('1 Elephant &')


If(@sortOrder='ASC')
BEGIN
SELECT * FROM @Sorting ORDER BY  
			   CASE 
               WHEN PATINDEX('[0-9]%', BusinessName)=1
                 THEN LEFT(BusinessName,PATINDEX('[0-9]%',BusinessName)) 
               End asc,
			   case when PATINDEX('[^0-9a-zA-Z]%', BusinessName)=1
			   Then LEFT(BusinessName,PATINDEX('[^0-9a-zA-Z]%',BusinessName))	
			   end asc, BusinessName asc 		   
END
else 
BEGIN
SELECT * FROM @Sorting ORDER BY BusinessName desc 	,

			   case when PATINDEX('[^0-9a-zA-Z]%', BusinessName)=1
			   Then LEFT(BusinessName,PATINDEX('[^0-9a-zA-Z]%',BusinessName))	
			   end desc, 	
			   
			    CASE 
               WHEN PATINDEX('[0-9]%', BusinessName)=1
                 THEN LEFT(BusinessName,PATINDEX('[0-9]%',BusinessName)) 
               End desc
end
Posted
Updated 20-Jun-18 7:32am
v2

1 solution

Your case statements are hosed up. This will work:

SQL
If @sortOrder LIKE 'ASC%'
BEGIN
    SELECT * FROM @Sorting 
    ORDER BY CASE WHEN PATINDEX('[0-9]%', BusinessName)=1
                  THEN LEFT(BusinessName,PATINDEX('[0-9]%',BusinessName))
                  ELSE CASE WHEN PATINDEX('[^0-9a-zA-Z]%', BusinessName)=1
                            THEN LEFT(BusinessName,PATINDEX('[^0-9a-zA-Z]%',BusinessName))	
                            ELSE BusinessName 
                       END
             END ASC;
END
ELSE
BEGIN
    SELECT * FROM @Sorting 
    ORDER BY CASE WHEN PATINDEX('[0-9]%', BusinessName)=1
                  THEN LEFT(BusinessName,PATINDEX('[0-9]%',BusinessName))
                  ELSE CASE WHEN PATINDEX('[^0-9a-zA-Z]%', BusinessName)=1
                       THEN LEFT(BusinessName,PATINDEX('[^0-9a-zA-Z]%',BusinessName))	
                       ELSE BusinessName 
                       END
             END DESC;
END
   
v2
Comments
suman palla 21-Jun-18 3:28am
   
Thank you John for your solution. But I need the sort order by below criteria.

Ascending order should be Numbers, Special characters, Alphabetical

Descending order should be Alphabetical , Special characters, Numbers
#realJSOP 21-Jun-18 5:27am
   
So you can't take what I gave you and make that happen?

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