SQL Server: How do you remove punctuation from a field?

前端 未结 8 1272
甜味超标
甜味超标 2021-02-05 20:50

Any one know a good way to remove punctuation from a field in SQL Server?

I\'m thinking

UPDATE tblMyTable SET FieldName = REPLACE(REPLACE(REPLACE(FieldNa         


        
8条回答
  •  被撕碎了的回忆
    2021-02-05 21:10

    I am proposing 2 solutions

    Solution 1: Make a noise table and replace the noises with blank spaces

    e.g.

    DECLARE @String VARCHAR(MAX)
    DECLARE @Noise TABLE(Noise VARCHAR(100),ReplaceChars VARCHAR(10))
    SET @String = 'hello! how * > are % u (: . I am ok :). Oh nice!'
    
    INSERT INTO @Noise(Noise,ReplaceChars)
    SELECT '!',SPACE(1) UNION ALL SELECT '@',SPACE(1) UNION ALL
    SELECT '#',SPACE(1) UNION ALL SELECT '$',SPACE(1) UNION ALL
    SELECT '%',SPACE(1) UNION ALL SELECT '^',SPACE(1) UNION ALL
    SELECT '&',SPACE(1) UNION ALL SELECT '*',SPACE(1) UNION ALL
    SELECT '(',SPACE(1) UNION ALL SELECT ')',SPACE(1) UNION ALL
    SELECT '{',SPACE(1) UNION ALL SELECT '}',SPACE(1) UNION ALL
    SELECT '<',SPACE(1) UNION ALL SELECT '>',SPACE(1) UNION ALL
    SELECT ':',SPACE(1)
    
    SELECT @String = REPLACE(@String, Noise, ReplaceChars) FROM @Noise
    SELECT @String Data
    

    Solution 2: With a number table

    DECLARE @String VARCHAR(MAX)
    SET @String = 'hello! & how * > are % u (: . I am ok :). Oh nice!'
    
    ;with numbercte as
    (
     select 1 as rn
     union all
     select rn+1 from numbercte where rn','<','%','(',')',':','!','&','@','#','$')
    
    for xml path(''))X(FilteredData)
    

    Output(Both the cases)

    Data

    hello  how   are  u  . I am ok . Oh nice
    

    Note- I have just put some of the noises. You may need to put the noises that u need.

    Hope this helps

提交回复
热议问题