Replace Value

  • I am in need of replacing a certain value at a certain position within a string. how can i accomplish this? - thanks!

    value = GGHRTYDGJYQWSDFPPJGV

    i need to replace POSITION 15, with an '?'

  • Do you only want to replace "F" with "?" when it is in the 15th position, or the 15th character no matter what it is? When you said "replacing a certain value", it led me to believe the former, but your last sentence seems to lean towards the latter interpretation.

    Stuff(value, 15, 1, '?') should handle the latter just fine, and will also work for the former, if you test for the value first.

    Just be careful about its peculiarities if you use it to update tables, etc., such as returning empty strings when it runs across unexpected data. These can all be worked around.

    Alternatively, you can do your own concatenation using Left() and Substring().

  • HI,

    the value that i want to replace doesn't matter, its only the position. for this example i need to replace position 15, perhaps next week i will need to replace position 22.

    i will try your script..

    thanks

    ryan

  • SELECT LEFT(@Var, @Position -1) + '?' + SUBSTRING(@Var, @Position + 1, LEN(@Var) - @Position)

    _____________
    Code for TallyGenerator

  • Or

    SELECT STUFF(@Var,@Position,1,'?')


    * Noel

Viewing 5 posts - 1 through 4 (of 4 total)

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