删除SQL中括号内的文本

时间:2017-10-02 18:44:45

标签: sql sql-server regex

我正在尝试删除其中的括号和文字。 例如:Column1具有数据,清理后的数据应该类似于column2

Column1       Column2
HF(abcd)      HF
BP(234)       BP
ATRS (2354)   ATRS
AB(PS) SD(12) AB SD

我正在尝试使用下面的正则表达式来查看已清理的数据,但我做错了

  

S /([^)] *)//

select Column1 from table where Column1 like '%s/\([^)]*\)//%'

这个

的最佳解决方案是什么?

2 个答案:

答案 0 :(得分:2)

这是对问题的原始版本的回答。

什么正则表达式?您只需要// Gather functions in an array, easier to loop trough var links = [ safety, daily, monday, tuesday, wednesday, thursday ] function cyclelinks() { links.forEach(function(link, index) { var delay = index * 5000; var fn = links[index]; setTimeout(fn, delay) }); } var intervalId; function toggleInterval() { var btn = document.getElementById("logo"); if (!intervalId) { var delay = links.length * 5000; // repeat after all functions are called with 5 sec delay cyclelinks() intervalId = setInterval(cyclelinks, delay); } else { clearInterval(intervalId); intervalId = null; location.reload(); } } 之前的所有内容:

(

如果你可能没有开场白,你可以这样做:

select left(column1, charindex('(', column1) - 1)

答案 1 :(得分:1)

使用Common Table Expression (CTE)尝试UNION,如下所示:

表格数据

enter image description here

;WITH cte
AS (
    SELECT NAME AS [Column1]
        ,CASE 
            WHEN charindex('(', NAME) > 0
                AND charindex(')', NAME) > charindex('(', NAME)
                THEN stuff(NAME, charindex('(', NAME), charindex(')', NAME) - charindex('(', NAME) + 1, '')
            ELSE NAME
            END AS [Column2]
        ,0 AS [Level] FROM #TM

    UNION ALL

    SELECT [Column1]
        ,stuff([Column2], charindex('(', [Column2]), charindex(')', [Column2]) - charindex('(', [Column2]) + 1, '') AS [Column2]
        ,[Level] + 1 AS [Level]
    FROM cte
    WHERE charindex('(',[Column2]) > 0
    )
    ,cte1
AS (
    SELECT *
        ,row_number() OVER (
            PARTITION BY [Column1] ORDER BY [Level] DESC
            ) AS Rn
    FROM cte
    )
SELECT Column1, Column2
FROM cte1 where Rn = 1

结果:

enter image description here