the varchar is defaulting to length one
DECLARE @Test1 varchar;
try this, which will uses a simple function that takes a sql_variant and returns the data type info back:
CREATE FUNCTION [dbo].[yourFunction]
(
@InputStr sql_variant --can not be varchar(max) or nvarchar(max)
)
returns
varchar(8000)
BEGIN
DECLARE @Value varchar(50)
--can use SQL_VARIANT_PROPERTY(@InputStr,'BaseType') to determine given datatype
--do whatever you want with @inputStr here
IF @InputStr IS NULL
BEGIN
SET @value= 'was null'
END
ELSE IF SQL_VARIANT_PROPERTY(@InputStr,'BaseType')='varchar'
BEGIN
--your special code here
SET @value= 'varchar('+CONVERT(varchar(10),SQL_VARIANT_PROPERTY(@InputStr,'MaxLength '))+') - '+CONVERT(varchar(8000),@InputStr)
END
ELSE IF SQL_VARIANT_PROPERTY(@InputStr,'BaseType')='datetime'
BEGIN
--your special code here
SET @value= 'datetime - '+CONVERT(char(23),@InputStr,121)
END
ELSE IF SQL_VARIANT_PROPERTY(@InputStr,'BaseType')='nvarchar'
BEGIN
--your special code here
SET @value= 'nvarchar('+CONVERT(varchar(10),CONVERT(int,SQL_VARIANT_PROPERTY(@InputStr,'MaxLength '))/2)+') - '+CONVERT(varchar(8000),@InputStr)
END
ELSE
BEGIN
--your special code here
set @value= 'unknown!'
END
RETURN @value
END
GO
DECLARE @Test1 varchar;
SET @Test1 = 'dog';
DECLARE @Test2 varchar(10);
SET @Test2 = 'cat';
SELECT @Test1 AS Result1, @Test2 AS Result2;
select [dbo].[yourFunction](@test1)
output:
Result1 Result2
------- ----------
d cat
(1 row(s) affected)
-------------------
varchar(1) - d
(1 row(s) affected)
moral of the story, don't be lazy, specify a length on all of your varchar values!!!