本文介绍了名称按数字,特殊字符,字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
推荐答案
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
这篇关于名称按数字,特殊字符,字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!