How (or why) do you have a stored procedure that includes CREATE FUNCTION statements?!
I'd start by searching this site for string splitter functions. The ones in your codes will run very slow on long strings or large result sets. I'd also consider how the code is laid out, because wading through that isn't a pleasant experience and is likely to discourage people from helping you.