Find non-ASCII characters in varchar columns using SQL Server

前端 未结 8 1560
遥遥无期
遥遥无期 2020-12-02 14:38

How can rows with non-ASCII characters be returned using SQL Server?
If you can show how to do it for one column would be great.

I am doing something like this

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 14:41

    I've been running this bit of code with success

    declare @UnicodeData table (
         data nvarchar(500)
    )
    insert into 
        @UnicodeData
    values 
        (N'Horse�')
        ,(N'Dog')
        ,(N'Cat')
    
    select
        data
    from
        @UnicodeData 
    where
        data collate LATIN1_GENERAL_BIN != cast(data as varchar(max))
    

    Which works well for known columns.

    For extra credit, I wrote this quick script to search all nvarchar columns in a given table for Unicode characters.

    declare 
        @sql    varchar(max)    = ''
        ,@table sysname         = 'mytable' -- enter your table here
    
    ;with ColumnData as (
        select
            RowId               = row_number() over (order by c.COLUMN_NAME)
            ,c.COLUMN_NAME
            ,ColumnName         = '[' + c.COLUMN_NAME + ']'
            ,TableName          = '[' + c.TABLE_SCHEMA + '].[' + c.TABLE_NAME + ']' 
        from
            INFORMATION_SCHEMA.COLUMNS c
        where
            c.DATA_TYPE         = 'nvarchar'
            and c.TABLE_NAME    = @table
    )
    select
        @sql = @sql + 'select FieldName = ''' + c.ColumnName + ''',         InvalidCharacter = [' + c.COLUMN_NAME + ']  from ' + c.TableName + ' where ' + c.ColumnName + ' collate LATIN1_GENERAL_BIN != cast(' + c.ColumnName + ' as varchar(max)) '  +  case when c.RowId <> (select max(RowId) from ColumnData) then  ' union all ' else '' end + char(13)
    from
        ColumnData c
    
    -- check
    -- print @sql
    exec (@sql)
    

    I'm not a fan of dynamic SQL but it does have its uses for exploratory queries like this.

提交回复
热议问题