do a replace in update statement

  • I have a varchar(130) field called exportpath. I want to replace the word devserver with prodserver where ever it finds that string

    in the field.  Can it be ran with a select first to see and verify the output before doing the update?

    data:

    \\devserver\exporta\filename.txt

    \\netloc\exporta\devserver\filename.txt

    After update:

    \\prodserver\exporta\filename.txt

    \\netloc\exporta\prodserver\filename.txt

    Thanks.

     

  • easy - a select with a where with a update with a replace and a where clause.

     

    what have you tried so far and what issues did you get ? this is basic sql.

  • not sure why I thought it was diffcult

    update tblset exportfilename = REPLACE(exportfilename , 'devserver', 'prodserver')

    where exportfilename like (%devserver%)

    Thanks sorry..

     

  • update tbl

    set exportfilename = REPLACE(exportfilename , 'devserver', 'prodserver')

    where exportfilename like (%devserver%)

  • Thanks to TSQL's support of updates with from clauses, I frequently write new or ad-hoc updates in this format (at least in development) to make testing easy:

    update tblset 
    set
    -- select *,
         exportfilename = REPLACE(exportfilename , 'devserver', 'prodserver')
    from tblset
    where exportfilename like '%devserver%';

    Just execute from select forward when you want to see the update. Execute the whole statement to actually update once effect is validated.

     

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

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