根据特定位置的特定字符串更新了行

时间:2015-10-13 11:51:57

标签: sql sql-server

我必须进行sql查询,该查询将更改在包含特定单词的所有行的特定文本的第二次出现/和第三次出现之间的特定文本。因此,让我们以下面的例子为例,如上所述放置的粗体字符串:

  

/ DUREN - RS7 / TAMBURLAGER /RS7-TAMB-COVER-2/IMG_9140.JPG

     

/ OHTER IO / MAN-SIX为56 / MACHINE4 / pic 56.jpg

     

/ DUREN - RS7 / Optislit Goebel /RS7-BARRIER-6/IMG_9141.JPG

     

/ DUREN - RS7 / Optislit Goebel /R-BARRIER-6/IMG_33.JPG

     

/ DUREN - RS7 / Optislit Goebel /RS7-BARRIER-7/IMG_9143.JPG

     

/ Cars / MaszynaAniaZawiera2 / Elementzawiera1 / IMG_0152 - Copy.JPG

     

/ Aanekoski Supercalander 2014 / SC4 Unwinder Operator   侧 /SC4-LF2/IMG_2486.JPG

     

/ DUREN - RS7 / Optislit Goebel /RS7-BARRIER-6/IMG_9142.JPG

     

/ DUREN - RS7 / Optislit Goebel /RS7-BARRIER-9/IMG_9148.JPG

     

/ OHTER IO / MAN-SIX为56 /MACHINE4/pic-11.jpg

     

/ OHTER IO / MAN-SIX为56 /MACHINE1/pic3.jpg

     

/ Aanekoski Supercalander 2014 / Supercalander 4 Drive   侧 /SC4-D-CL1/IMG_3769.JPG

     

/ Aanekoski Supercalander 2014 / Electriccabinets /EC1-1/IMG_2745.JPG

     

/ Aanekoski Supercalander 2014 / SC5Unwinder   驱动 /SC5-D-F1-SW1/IMG_5304.JPG

     

/ Cars / MaszynaAniaZawiera2 / Elementzawiera1 / IMG_0148 - Copy.JPG

现在 - 让我们只想找到那些字符串 MAN-SIX为56 ,然后将其更改为 WHATEVER-11

所以根据提到的例子,我们得到3行包含它:

  

/ OHTER IO / MAN-SIX为56 / MACHINE4 / pic 56.jpg

     

/ OHTER IO / MAN-SIX为56 /MACHINE4/pic-11.jpg

     

/ OHTER IO / MAN-SIX为56 /MACHINE1/pic3.jpg

所以最后根据示例将这些行更改为:

  

/ OHTER IO / WHATEVER-11 / MACHINE4 / pic 56.jpg

     

/ OHTER IO / WHATEVER-11 /MACHINE4/pic-11.jpg

     

/ OHTER IO / WHATEVER-11 / MACHINE1 / pic 3.jpg

如何以安全的方式实现这一目标,因为我获得了数百万条记录,而且必须足够安全,不要接触任何其他内容?

提前致谢。

有待进一步讨论:

 select 
    stuff([PicturePath], charindex('/', [PicturePath], charindex('/', [PicturePath]) + 1) + 1, 13, 'WHATEVER-11') as new_data
from [WojtGroup].[dbo].[tbElemPics]
where substring([PicturePath], charindex('/', [PicturePath], charindex('/', [PicturePath]) + 1) + 1, 14) = 'MAN-SIX as 56/'

2 个答案:

答案 0 :(得分:1)

您可以使用CHARINDEX STUFF ;

执行此操作

选择语句sqlfiddle

select 
    stuff(data, charindex('/', data, charindex('/', data) + 1) + 1, 13, 'WHATEVER-11') as new_data
from tbl
where substring(data, charindex('/', data, charindex('/', data) + 1) + 1, 14) = 'MAN-SIX as 56/'

更新语句sqlfiddle

update tbl
set data = stuff(data, charindex('/', data, charindex('/', data) + 1) + 1, 13, 'WHATEVER-11')
where substring(data, charindex('/', data, charindex('/', data) + 1) + 1, 14) = 'MAN-SIX as 56/'

答案 1 :(得分:1)

试试这个,

DECLARE @string      VARCHAR(100)=N'/OHTER IO/MAN-SIX as 56/MACHINE4/pic 56',
        @replacewith VARCHAR(100)=N'WHATEVER-11'

SELECT Substring(@string, Charindex('/', @string, Charindex('/', @string)+1)
                          + 1, Charindex('/', @string, Charindex('/', @string, Charindex('/', @string)+1)
                                                       + 1) - Charindex('/', @string, Charindex('/', @string) + 1) - 1) AS substring
SELECT @string                                                                                                                    AS ActualData,
       Stuff(@string, Charindex('/', @string, Charindex('/', @string)+1)
                      + 1, Charindex('/', @string, Charindex('/', @string, Charindex('/', @string)+1)
                                                   + 1) - Charindex('/', @string, Charindex('/', @string) + 1) - 1, @replacewith) AS ReplacedData

准备尝试使用此选项来测试您的方案。

select Stuff(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                      + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                                                      + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1, 'WHATEVER-11')
                                                                      from tbElemPics
WHERE  Substring(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                             + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                                             + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1) = 'MAN-SIX as 56'         

使用此更新声明

UPDATE tbElemPics
SET    PicturePath = Stuff(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                      + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                                                      + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1, 'WHATEVER-11')
WHERE  Substring(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                             + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)
                                                             + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1) = 'MAN-SIX as 56'