SQL varchar 列的更新

SQL update for the varchar column

我想把AttachmentCopyLoc列的字符串从D:\IT\Public\FTX_RobotAlerts6改成V:\IT\Public\FTX_RobotAlerts6只是这里改的是D改成V,其余的字符串是一样的(我没事件想改变它)。

我该怎么做?

在此先感谢您的帮助。

确切的语法取决于平台,但它类似于

UPDATE {table}
SET AttachmentCopyLoc = REPLACE ( AttachmentCopyLoc , 'D:' , 'V:' )  
WHERE AttachmentCopyLoc LIKE 'D:%'

另一种方法是基本的字符串操作

UPDATE {table}
SET AttachmentCopyLoc ='V' + substring(AttachmentCopyLoc,2,len(AttachmentCopyLoc )) 
where AttachmentCopyLoc like 'D%'

在SQL服务器中,我建议使用stuff():

update t
    set AttachmentCopyLoc = stuff(AttachmentCopyLoc, 1, 1, 'V')
    where AttachmentCopyLoc like 'D:%';

这个版本有两个优点:

  1. STUFF() 的使用确保仅替换第一次出现的 'D:'。诚然,该子字符串不太可能在列中多次出现,但为什么要冒险呢?
  2. 使用 LIKE 允许使用 AttachmentCopyLoc 上的索引,如果索引可用并且使用 hte 索引是合适的。

这只会改变字符串的第一个字母:

update <tablename>
set AttachmentCopyLoc = 'V'+substring(AttachmentCopyLoc,2,len(AttachmentCopyLoc)-1)