From KM's comment above...
I know you didn't state it is comma seperated, but if it was a CSV or even if you have it space seperated you could do the following.
DECLARE @SomeTest varchar(100) --used to hold your values
SET @SomeTest = (SELECT '68,72,103') --just some test data
SELECT
LoginID --change to your column names
FROM
Login --change to your source table name
INNER JOIN
( SELECT
*
FROM fn_IntegerInList(@SomeTest)
) n
ON
n.InListID = Login.LoginID
ORDER BY
n.SortOrder
And then create fn_IntegerInList()
:
CREATE FUNCTION [dbo].[fn_IntegerInList] (@InListString ntext)
RETURNS @tblINList TABLE (InListID int, SortOrder int)
AS
BEGIN
declare @length int
declare @startpos int
declare @ctr int
declare @val nvarchar(50)
declare @subs nvarchar(50)
declare @sort int
set @sort=1
set @startpos = 1
set @ctr = 1
select @length = datalength(@InListString)
while (@ctr <= @length)
begin
select @val = substring(@InListString,@ctr,1)
if @val = N','
begin
select @subs = substring(@InListString,@startpos,@ctr-@startpos)
insert into @tblINList values (@subs, @sort)
set @startpos = @ctr+1
end
if @ctr = @length
begin
select @subs = substring(@InListString,@startpos,@ctr-@startpos)
insert into @tblINList values (@subs, @sort)
end
set @ctr = @ctr +1
set @sort = @sort + 1
end
RETURN
END
This way your function creates a table that holds a sort order namely, SortOrder
and the ID or number you are passing in. You can of course modify this so that you are looking for space
rather then ,
values. Otherwise Martin has the right idea in his answer. Please note in my example I am using one of my tables, so you will need to change the name Login
to whatever you are dealing with.