create table [Order] (
ID int, Name varchar(32)
)
insert into [Order]
values (1, 'Fruits'),
(33, 'Liquor'),
(48, 'Tennis bats'),
(58, 'Vegetables'),
(95, 'Poker cards')
GO
create function dbo.Orderer(@id int, @orderString varchar(1024))
returns int
as
begin
set @orderString = replace(',' + @orderString + ',', ' ', '');
declare @pos int,
@str varchar(1024),
@itemOrder int;
set @pos = charindex(',' + cast(@id as varchar(16)) + ',', @orderString);
if @pos != 0
begin
set @orderString = substring(@orderString, 1, @pos + 1);
set @pos = charindex(',', @orderString);
set @itemOrder = 0;
while @pos != 0
begin
set @itemOrder += 1;
set @pos = charindex(',', @orderString, @pos + 1);
end
return @itemOrder
end
return null
end
GO
select * from [Order]
order by dbo.Orderer(ID, '48, 95, 33, 1, 58')