using a sql statement to do a string replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bj daniels

    #1

    using a sql statement to do a string replace

    I am not certain which group to post this to - sorry if this is the wrong
    group:

    I have a table that holds the location of various media and such (mostly
    images). One field is the Media name, the other is the URL in the form
    http://myServer.gunnery.org/media/pic1 for example. we are about to move
    all the media to a new location (new server). the paths will remain the
    same except for the server name.

    Is there an update statement that will let me go through the table an
    replace myServer with newServer in all the URLs (and leave the rest of the
    URL alone). So the link above would become
    http://newServer.gunnery.org/media/pic1. I know how I would do it with VB,
    but I am not as familar with all the tricks in SQL.

    thanks

    bj daniels
    danielsb@gunner y.org


  • Keith Kratochvil

    #2
    Re: using a sql statement to do a string replace

    You should be able to use the REPLACE function. This select statement should show you that it works as expected:

    SELECT REPLACE(MediaNa me, 'myServer.gunne ry', 'newserver.gunn ery') FROM YourTable


    After verifying that the select gives you the expected result, run the following update to update the data within the table:

    --UPDATE YourTable SET MediaName = REPLACE(MediaNa me, 'myServer.gunne ry', 'newserver.gunn ery')
    --SELECT @@rowcount AS RowsUpdated

    --
    Keith

    "bj daniels" <danielsb@gunne ry.org> wrote in message news:eNjDE50ZDH A.1620@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I am not certain which group to post this to - sorry if this is the wrong
    > group:
    >
    > I have a table that holds the location of various media and such (mostly
    > images). One field is the Media name, the other is the URL in the form
    > http://myServer.gunnery.org/media/pic1 for example. we are about to move
    > all the media to a new location (new server). the paths will remain the
    > same except for the server name.
    >
    > Is there an update statement that will let me go through the table an
    > replace myServer with newServer in all the URLs (and leave the rest of the
    > URL alone). So the link above would become
    > http://newServer.gunnery.org/media/pic1. I know how I would do it with VB,
    > but I am not as familar with all the tricks in SQL.
    >
    > thanks
    >
    > bj daniels
    > danielsb@gunner y.org
    >
    >[/color]

    Comment

    • bj daniels

      #3
      Re: using a sql statement to do a string replace

      Looks Perfect - thanks!


      "Aaron Bertrand - MVP" <aaron@TRASHasp faq.com> wrote in message
      news:ePp4370ZDH A.2136@TK2MSFTN GP10.phx.gbl...[color=blue]
      > UPDATE table SET mediaName = REPLACE(mediaNa me, '//myServer',[/color]
      '//newServer')[color=blue]
      > WHERE CHARINDEX('//myServer', mediaName) > 0
      >
      >
      > "bj daniels" <danielsb@gunne ry.org> wrote in message
      > news:eNjDE50ZDH A.1620@TK2MSFTN GP12.phx.gbl...[color=green]
      > > I am not certain which group to post this to - sorry if this is the[/color][/color]
      wrong[color=blue][color=green]
      > > group:
      > >
      > > I have a table that holds the location of various media and such (mostly
      > > images). One field is the Media name, the other is the URL in the form
      > > http://myServer.gunnery.org/media/pic1 for example. we are about to[/color][/color]
      move[color=blue][color=green]
      > > all the media to a new location (new server). the paths will remain the
      > > same except for the server name.
      > >
      > > Is there an update statement that will let me go through the table an
      > > replace myServer with newServer in all the URLs (and leave the rest of[/color][/color]
      the[color=blue][color=green]
      > > URL alone). So the link above would become
      > > http://newServer.gunnery.org/media/pic1. I know how I would do it with[/color]
      > VB,[color=green]
      > > but I am not as familar with all the tricks in SQL.
      > >
      > > thanks
      > >
      > > bj daniels
      > > danielsb@gunner y.org
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...