如何在SQL中将特定字符串部分剪切到另一列?

时间:2016-03-22 13:20:04

标签: sql-server tsql sql-server-2012

我在一张包含nvarchar列的表格中有大约500条记录。

我想部分数据剪切到另一列。通过“剪切”我的意思是在原始列中删除它并将其添加到目标列。

所有必须删除的数据都包含在括号内。括号内的文本可能出现在字符串中的任何位置。

例如,ColumnA具有:SomeTest Data [I want to cut this],我想将[I want to cut this](但没有括号)移动到ColumnB。

我如何实现这一目标?

更新

最终发现了它。问题是我没有逃过我的括号 我现在(和工作):

UPDATE TableA
SET TargetColumn = substring(SourceColumn,charindex('[',SourceColumn)+1,charindex(']',SourceColumn)-charindex('[',SourceColumn)-1),
SourceColumn = substring(SourceColumn, 0, charindex('[',SourceColumn))
where TableA.SourceColumn like '%\[%\]%' ESCAPE '\'

3 个答案:

答案 0 :(得分:3)

这些行中的UPDATE语句可以做到:

CREATE TABLE #Test
(
    StringToCut VARCHAR(100)
    ,CutValue VARCHAR(100)
)

INSERT #Test
VALUES
('SomeTest Data 1 [I want to cut this 1] More Testing',NULL),
('SomeTest Data 2 [I want to cut this 2]',NULL),
('SomeTest Data 3 [I want to cut this 3] Additional Test',NULL),
('[I want to cut this 4] last test',NULL)

SELECT * FROM #Test

--Populate CutValue column based on starting position of '[' and ending position of ']'
UPDATE #Test
SET CutValue = SUBSTRING(StringToCut,CHARINDEX('[',StringToCut),(CHARINDEX(']',StringToCut)-CHARINDEX('[',StringToCut)))

--Remove the '[' ']'
UPDATE #Test
SET CutValue = REPLACE(CutValue,'[','')
UPDATE #Test
SET CutValue = REPLACE(CutValue,']','')

--Remove everything after and including '[' from StringToCut
UPDATE #Test
SET StringToCut = LEFT(StringToCut,CHARINDEX('[',StringToCut)-1) + LTRIM(RIGHT(StringToCut,LEN(StringToCut)-CHARINDEX(']',StringToCut)))

SELECT * FROM #Test

DROP TABLE #Test

答案 1 :(得分:1)

你留下了一些未回答的问题。

  • 您想如何处理NULL值?我要把它们留下来。
  • 'cut'字符串应该去哪里?我假设“在最后”
  • 如果找到嵌套括号,你会怎么做? [[cut me]]
  • 您是否需要移除任何周围空间?例如,“猫[睡觉]睡觉”会在“睡觉”之前变为“猫咪睡觉”两个空格吗?

要使操作成为原子,您需要使用单个UPDATE 这是一个示例脚本,可以帮助您入门。

--build a temp table with sample data
declare @t table(ikey int, sourcecolumn nvarchar(100), targetcolumn nvarchar(100));
insert into @t
      select 0,'SomeTest Data [I want to cut this]','Existing Data For Row 1'
union select 1,'SomeTest [cut this too] Data2','Existing Data For Row 2'
union select 2,'[also cut this please] SomeTest Data3',null
union select 3,null,null
union select 4,null,''
union select 5,'Nested bracket example [[[within nested brackets]]] Other data',null
union select 6,'Example with no brackets',null
union select 7,'No brackets, and empty string in target',''

--show "before"
select * from @t order by ikey

--cut and paste
update @t
set
  targetcolumn =
    isnull(targetcolumn,'') +
      case when 0 < isnull(charindex('[',sourcecolumn),0) and 0 < isnull(charindex(']',sourcecolumn),0)
           then substring(sourcecolumn,charindex('[',sourcecolumn)+1,charindex(']',sourcecolumn)-charindex('[',sourcecolumn)-1)
           else ''
           end
 ,sourcecolumn =
    case when sourcecolumn is null
         then null 
         else substring(sourcecolumn,0,charindex('[',sourcecolumn)) + substring(sourcecolumn,charindex(']',sourcecolumn)+1,len(sourcecolumn))
         end
where sourcecolumn like '%[%'
  and sourcecolumn like '%]%'

--show "after"
select * from @t order by ikey

答案 2 :(得分:0)

另一个在单个更新声明中 -

CREATE TABLE #Test
(
    StringToCut VARCHAR(50)
    ,CutValue VARCHAR(50)
)

INSERT #Test
VALUES
('SomeTest Data 1 [I want to cut this 1]',NULL),
('SomeTest Data 2 [I want to cut this 2]',NULL),
('SomeTest Data 3 [I want to cut this 3]',NULL),
('SomeTest Data 4 [I want to cut this 4]',NULL)

UPDATE #Test
SET CutValue = 
     SUBSTRING(StringToCut, CHARINDEX('[', StringToCut)+1, CHARINDEX(']', StringToCut) - CHARINDEX('[', StringToCut) - 1)

SELECT * FROM #Test