Using SUBTRING For a Destination Field

  • I have a Function where I want to put a character in a particular position in a Destination string. For example, I have a 5 character field containing, say, "12345". I would like to put the letter "A" in the third position resulting in "12A45". I'm trying the following

    DECLARE @RetVal as varchar(5)

    SET SUBSTRING(@RetVal,3,1) = 'A'

    I'm getting a compiler error

    "incorrect syntax near '@RetVal'. Expecting '(' or SELECT"

    Am I using the wrong Function to do this? If so, what should I be using?

    Thanks, Eddie

  • You can STUFF it!

    DECLARE @Val1 varchar(5) = '12345'

    declare @RetVal varchar(10)

    set @RetVal = STUFF(@Val1,3,1,'A')

    select @Val1

    , @RetVal

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • That worked... thanks, Eddie

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

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