I have a table value function in TSQL that works well if I pass a string but not if I pass a varchar.
For example, this works:
SELECT * from [dbo].[SplitString]('ta;aa;qq', ';')

This does not work:
declare @invarchar as varchar
set @invarchar = 'ta;aa;qq'
SELECT * from [dbo].[SplitString](@invarchar, ';')

If you are curious about the function:
CREATE FUNCTION [dbo].[SplitString]
(
@List NVARCHAR(MAX),
@Delim VARCHAR(255)
)
RETURNS TABLE
AS
RETURN ( SELECT [Value] FROM
(
SELECT
[Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_objects) AS x
WHERE Number <= LEN(@List)
AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
) AS y
);
GO
I have the same result with multiple table value function, so I guess that my problem is in the way I call it from the variable that is wrong. How can I use a variable and still have the same output?