To do this you will first need to parse your delimited string. The easiest way to parse a delimited string like is to use DelimitedSplit8K. You can find out about this splitter by following the article in my signature about splitting strings.
In this case we need to split the string and keep them in order. Here is where we can really harness the power of the DelimitedSplit8K function. The following code should work for you.
DECLARE @test1 TABLE (letter varchar(1))
INSERT INTO @test1(letter) VALUES('a')
INSERT INTO @test1(letter) VALUES('b')
INSERT INTO @test1(letter) VALUES('c')
INSERT INTO @test1(letter) VALUES('d')
INSERT INTO @test1(letter) VALUES('e')
declare @SortString varchar(10) = 'd;c;b;a;e'
select *
from @test1 t
join
(
select ItemNumber, Item
from dbo.DelimitedSplit8K(@SortString, ';')
) x on t.letter = x.Item
order by x.ItemNumber
Make sure you read that article and understand what it is doing.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/