Вы можете использовать Скалярную функцию (больше информации о скалярных функциях здесь ), которая удаляет буквы из названий ваших категорий (больше информации об этой конкретной функции здесь ) и используйте результат для заказа ваших данных:
--1. declare a new function that removes all letters from your category name
--1.1 if the function already exists delete it
if OBJECT_ID('fx_remove_letters') is not null
drop function fx_remove_letters
go
--1.2 create a function to remove all letters (CHAR(65) is letter A, char(90) is letter Z)
create function fx_remove_letters(@str NVARCHAR(max))
returns NVARCHAR(max)
as
begin
DECLARE @loop INT
SET @loop = 0
WHILE @loop < 26
BEGIN
SET @str = REPLACE(@str, CHAR(65 + @loop), '')
SET @loop = @loop + 1
END
return @str
end
go
--2. this table variable holds your categories in a random order
declare @tmp table (category_name nvarchar(10))
--3. populate the test table variable
insert into @tmp
values('6D'),('2'),('3'),('12'),('5'),('6C'),('6B'),
('1'),('7A'),('4'),('7B'),('10'),('11'),('6A')
--4. select your data ordering them with the function we defined at the beginning
select category_name
from @tmp
order by cast(dbo.fx_remove_letters(category_name) as int), category_name
Вот таблица перед заказом:
![enter image description here](https://i.stack.imgur.com/1NpVd.png)
Теперь результаты после order by
:
![enter image description here](https://i.stack.imgur.com/3Oy3B.png)