Update Query

  • Hi,

    I am looking to update a field in an SQL DB. Then thing is its a UNC path and I only want to update the first part. e.g.

    Change \\server1\folder1\1234 to \\server2\folder5\1234 (Keeping the original folder names(the 1234 part as this is an account number). There are thousands of records and I need to select records on the folder part of the UNC path. I dont think I am able to do it!

    Any help would be most helpful!

    Mark.

  • can you post some data samples ?

    this is a wild guess

    Update Table Set Fld = '\\server2\folder5\' + Right(Fld,charindex('\',1,reverse(fld)) -1)

     


    * Noel

  • Try using the replace function.

    Update Mytable

    set MyField = replace( MyField, '\\server1\folder1\1234', '\\server2\folder5\1234')

    where MyField = '\\server1\folder1\1234'

  • \\SERVER1\SCANFILES2\JPG SCANNING\PREVIEW TEST

    \\SERVER1\SCANFILES2\JPG SCANNING\PREVIEW TEST

    \\SERVER1\SCANFILES2\JPG SCANNING\PREVIEW TEST

    \\SERVER1\SCANFILES2\JPG SCANNING\PREVIEW TEST

    \\SERVER1\SCANFILES2\JPG SCANNING\PREVIEW TEST

    Putting some thought into it its probably going to be easier just to update the server name to for example \\myserver and remove the scanfiles2 folder name so it would read

     \\myserver\JPG SCANNING\PREVIEW TEST

    Sorry for the confusion!

    So what I have got so far is :-

    select FilePath

    from dbo.BulkImages

    where filepath LIKE N'%JPG SCANNING%'

    To select everything with JPG SCANNING in the UNC path but I cant work out how the replace function fits in!

    Many Thanks

    Mark.

  • SORTED!!!!

     

    Update dbo.BulkImages

    set FilePath = replace( FilePath, '\\server1\scanfiles2', '\\NAS1')

    where filepath LIKE N'%JPG SCANNING%

     

    Thanks guys!!!!

  • Well, it depends on what precisely you want to do. If you just want to replace a string that is known, this is the solution:

    select FilePath, REPLACE(FilePath,'SERVER1','MYSERVER') as NewFilePath

    from dbo.BulkImages

    where filepath LIKE N'%JPG SCANNING%'

    But if there can be several different servers in the path, and you want to replace the server with 'MYSERVER' - no matter what it was originally - or to modify the path somehow, then you'll have to play around with it, locate the \ characters using CHARINDEX and then replace the corresponding part or create several strings and concatenate them. You could use this as a starting point:

    select FilePath,

    charindex('\',RIGHT(filepath,LEN(filepath)-2)) as pos, /*position of first \ after the starting double \*/

    RIGHT (filepath, LEN(filepath)- charindex('\',RIGHT(filepath,LEN(filepath)-2))-2) as [path without server],

    '\\MYSERVER\'+RIGHT (filepath, LEN(filepath)- charindex('\',RIGHT(filepath,LEN(filepath)-2))-2) as NewPath

    from dbo.BulkImages

    where filepath LIKE N'%JPG SCANNING%'

    EDIT: Well, that's how it is when one has to work... I wrote the post, but then i got several urgent phone calls - and when I posted it, it was too late well, maybe next time!

  • You gotta be quick around here, Between Noeld, and Remi, its hard to get a post in.

  • You had it easy on this one... I'm just monitoring it .

  • You are very swift too    so shouldn't complain around here

     


    * Noel

  • I hate to have to do this, again :

    73,32,97,103,114,101,101

    .

  • It would have been cooler if you use the Hex or maybe binary representation

     


    * Noel

  • Maybe but you still got the message .

Viewing 12 posts - 1 through 12 (of 12 total)

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