Add/Insert a character mid string

  • Hi

    I'm sure someone will know an answer to this.

    I need to insert a decimal point (.) as the forth character of an exising field

    example

    A1234 will become A12.34

    B98765 will become B98.765

    Many Thanks in advance

  • declare @test-2 varchar(10)

    set @test-2='A1234'

    Select STUFF(@test,4,0,'.')

    Thanks

  • Take a look at the substring function

    /*

    A1234 will become A12.34

    B98765 will become B98.765

    */

    DECLARE @String1 VARCHAR(10)

    DECLARE @String2 VARCHAR(10)

    SELECT @String1 = 'A1234',

    @String2 = 'B98765'

    SELECT SUBSTRING(@String1,1,3) + '.' + SUBSTRING(@String1,4,LEN(@String1)-3),

    SUBSTRING(@String2,1,3) + '.' + SUBSTRING(@String2,4,LEN(@String2)-3)

  • Hi,

    You can use this Query to add an character in between the value of an existing field

    Select STUFF(ColumnName,4,0,'.') from TableName

    Hope this helps.. 😉

    Thanks

    Kivan.G

  • many thanks, works a treat.

    P

  • Stuff is good. 🙂

    This is just another alternative:

    Declare @string varchar(10) = 'B9876543'

    Select (LEFT(@string, 3) + '.' + RIGHT(@string, (Len(@String) - 3)))

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

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

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