function to replace comma, then loop

hello all,
I'm looking to have a user defined function or a function i can include in a new field in the table itself based on the existing field named: code

i have a table with a field with the following data:
code = "AB, BC, CD"
this field has variable length. so there could be x number of codes.

i would like to convert the field to: <a href='mypage.asp?code=' & code1 & "'>" & code1 & "</a>"
and then loop thru all codes.

thank you all.
LVL 9
nito8300Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dbbishopCommented:
So, in your above example, are you saying you want the final result to be:
<a href='mypage.asp?code=AB'>AB</a><a href='mypage.asp?code=BC'>BC</a><a href='mypage.asp?code=CD'>CD</a>

0
dbbishopCommented:
This function returns a table variable. You use it as:

dbo.SplitList(code, ',')

In a real example, you would code:

DECLARE @html VARCAHR(8000)

SELECT @html = coalesce(@html, '') + '<a href="mypage.asp?code=' + ListItem + '">' + ListItem + '</a>'
FROM dbo.SplitList(@code, ',')

@html will have the concatenated code.

=========================================================
CREATE  FUNCTION SplitList (@List VARCHAR(4000), @Delimiter CHAR(1) = ',')
       RETURNS @tblList TABLE (ListItem VARCHAR(100), UNIQUE CLUSTERED (ListItem))
AS
   BEGIN
      DECLARE @pos INT
      DECLARE @ListItem VARCHAR(4000)
     
      SET @List = LTRIM(RTRIM(@List))
      IF RIGHT(@List, 1) = @Delimiter
          SET @List = LEFT(@List, Len(@List) - 1)
      
      SET @pos = CHARINDEX(@Delimiter, @List)

      WHILE @pos > 0
          BEGIN
            SET @ListItem = LEFT(@List, CHARINDEX(@Delimiter, @List) - 1)
            INSERT INTO @tblList (ListItem) VALUES(RTRIM(LTRIM(@ListItem)))
            SET @List = SUBSTRING(@List, @pos + 1, LEN(@List))
            SET @pos = CHARINDEX(@Delimiter, @List)
          END
      RETURN
    END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.