Insert character question

  • I need to insert a period in a varchar column always 3 "places" over from the right side of the value for example XXXX.XX

    Hoping someone can clarify for me the best way (if any) to accomplish this?

    Thanks in advance!

  • hansontd (1/8/2010)


    I need to insert a period in a varchar column always 3 "places" over from the right side of the value for example XXXX.XX

    I suspect there might be a better way to do this, but off the top of my head, try:update

    set [column] = left([column], len([column]) - 2 + '.' + right([column], 2)

    Obviously, replace

    and [column] with your table and column names.

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

  • Great! Thank you very much for your help!!

  • Here it is using STUFF.

    DECLARE @Var varchar(10)

    SET @Var = 'XXXXXXXX'

    SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • John Rowan (1/8/2010)


    Here it is using STUFF.

    DECLARE @Var varchar(10)

    SET @Var = 'XXXXXXXX'

    SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')

    Ah, yes. I defer to those who have more SQL experience than I do! 🙂

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

  • Thank you both very much for your help with my issue!! 😀

  • Ray K (1/8/2010)


    John Rowan (1/8/2010)


    Here it is using STUFF.

    DECLARE @Var varchar(10)

    SET @Var = 'XXXXXXXX'

    SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')

    Ah, yes. I defer to those who have more SQL experience than I do! 🙂

    I don't see STUFF used as often as it can be. It is a seemingly little known function that can be very handy with string manipulations. It can work similar to REPLACE and overwrite a section of the string, or insert new values into the string....leading to the word STUFF as in like a turkey!

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • I've used the STUFF function numerous times in the past but just couldn't wrap my mind around how to work it in this case....I've always dealth with strings and worked with stuff from the left side of the string whereas in this case I needed to work back from the right...

    Thanks again to you both for taking the time to jump in and lend a hand!!

  • hansontd (1/9/2010)


    I've used the STUFF function numerous times in the past but just couldn't wrap my mind around how to work it in this case....I've always dealth with strings and worked with stuff from the left side of the string whereas in this case I needed to work back from the right...

    Thanks again to you both for taking the time to jump in and lend a hand!!

    Try REVERSE() 😎


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • I had tried utilzing REVERSE() with stuff but for whatever reason I guess I wasn't utilizing it correctly as I never got teh results I was after...but thank for the suggestion....

  • hansontd (1/10/2010)


    I had tried utilzing REVERSE() with stuff but for whatever reason I guess I wasn't utilizing it correctly as I never got teh results I was after...but thank for the suggestion....

    SELECT REVERSE(STUFF(REVERSE(@Var), 4, 0, '.'))


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

Viewing 11 posts - 1 through 10 (of 10 total)

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