I am trying to cast something to char(n) where n is a function argument
ALTER FUNCTION FixMe(@colName varchar, @width integer) RETURNS varchar
AS BEGIN
RETURN CAST(@colName as char(@width))
END
This code is giving an error of
Incorrect syntax near '@width'.
I have also tried executing this with EXEC() via:
EXEC('set @retval = CAST(@colName as char(' + @width + '))')
But I then run in to
Invalid use of side-effecting or
time-dependent operator in 'EXECUTE
STRING' within a function.
Even if you did manage to get this to work in a function your
RETURNS varcharstatement would cause the result to be implicitly converted tovarchar(1)on the way out.I assume this is related to your previous question in which case this might work better for you.