Sunday, February 12, 2012

Converting problem (Stored Procedure)

I need to link together string (NT-) and integer variable (1). The value of variable2 should be NT-1. I always got such an error message:

Conversion failed when converting the varchar value 'NT-1' to data type int.

DECLARE @.variable1 int;
SET @.variable1 = 1;

DECLARE @.variable2 varchar(10);
SET @.variable2 = CONVERT(varchar, 'NT-1')+ CONVERT(varchar, @.variable1);

i tried the code that you posted and do not encounter the conversion error.

However, you can simply the code and remove the convert for 'NT-1' as it is a string so you do not need to convert to varchar. And for the @.variable1, you might want to specify the varchar size or it will defaulted to a certain size. (cannot remember what is the default size)
SET @.variable2 = 'NT-1' + CONVERT(varchar(10), @.variable1)

No comments:

Post a Comment