SQL Server删除从第二次出现到结束的所有内容

时间:2017-03-17 12:51:07

标签: sql sql-server match

我的SQL Server表中有一个包含文本的列,假设一行包含以下文本:

"this is a test that was made the year 2012 cause in the year 2013 we was down"

我需要检查一年是否出现不止一次;如果是,则应将文本更新为:

"this is a test that was made the year 2012 cause in the "

所以我需要在第二次出现之前保留所有内容并删除从上次发生到结束的所有内容

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

这很痛苦,但你可以这样做:

select (case when col like '%year%year%'
             then left(col,
                       charindex('year', col, charindex('year', col) + 4) - 1
                      )
             else col
        end) as first_year_only