Сортировка имен по номерам, специальным символам, алфавитному порядку
Привет,
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
Решение, которое я устал, не работало . Может ли кто-нибудь, пожалуйста, предоставить решение.
Что я уже пробовал:
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