Why is SQL Server ignoring me?!

  • I have a SP that includes a cursor. The cursor is used to look for a reference (combination of INT and VARCHAR), choose the reference with the highest number (last 3 digits of reference), add 1 to the sequence and then return the new reference as an OUTPUT.

    Why, when casting the last three digits as a VARCHAR does SQL server convert them back to INT. It definately does this as I have tested it with:

    ISNUMERIC(variableName)

    Which returns 1(true, it is a number). Even though it is a number, I dont want it to be as I am trying to concatenate it to a VARCHAR to make the complete OUTPUT variable.

    Does this make sense?

    how can I stop SQL Server from converting the value to an INT?

    Thanks in advance.

    Carl.

  • Using cast will convert it to a varchar, however even varchar datatype's will return 1 to a isnumeric() if it can evaluated into a number. Casting it to a varchar should still allow you to concatinate it onto another varchar...

    Does that help....?

  • If you 'add' two values of different data type, SQL Server will convert one of them so they are both the same.

    I don't know the exact reason why SQL Server chooses to convert either one of the parameters, but it's always a good idea to do an explicit cast.

    
    
    DECLARE @intvar int
    DECLARE @stringvar varchar(5)
    DECLARE @result varchar(6)
    SET @intvar = 2
    SET @stringvar = '1'
    SET @result = @intvar + @stringvar
    PRINT @result
    SET @result = cast(@intvar as varchar) + @stringvar
    PRINT @result

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply