Find all special characters in a column in SQL Server 2008

后端 未结 4 562
梦如初夏
梦如初夏 2020-12-24 08:31

I need to find the occurrence of all special characters in a column in SQL Server 2008. So, I don\'t care about A, B, C ... 8, 9, 0, but I do care

相关标签:
4条回答
  • 2020-12-24 09:10
    Select * from TableName Where ColumnName LIKE '%[^A-Za-z0-9, ]%'
    

    This will give you all the row which contains any special character.

    0 讨论(0)
  • 2020-12-24 09:13

    Negatives are your friend here:

    SELECT Col1
    FROM TABLE
    WHERE Col1 like '%[^a-Z0-9]%'
    

    Which says that you want any rows where Col1 consists of any number of characters, then one character not in the set a-Z0-9, and then any number of characters.

    If you have a case sensitive collation, it's important that you use a range that includes both upper and lower case A, a, Z and z, which is what I've given (originally I had it the wrong way around. a comes before A. Z comes after z)


    Or, to put it another way, you could have written your original WHERE as:

    Col1 LIKE '[!@#$%]'
    

    But, as you observed, you'd need to know all of the characters to include in the [].

    0 讨论(0)
  • 2020-12-24 09:20

    The following transact SQL script works for all languages (international). The solution is not to check for alphanumeric but to check for not containing special characters.

    DECLARE @teststring nvarchar(max)
    SET @teststring = 'Test''Me'
    SELECT 'IS ALPHANUMERIC: ' + @teststring
    WHERE @teststring NOT LIKE '%[-!#%&+,./:;<=>@`{|}~"()*\\\_\^\?\[\]\'']%' {ESCAPE '\'}
    
    0 讨论(0)
  • 2020-12-24 09:22
    select count(*) from dbo.tablename where address_line_1 LIKE '%[\'']%' {eSCAPE'\'}
    
    0 讨论(0)
提交回复
热议问题