Querying a SQL Server 2008 table to find values in a column containing Unicode characters

前端 未结 3 1396
深忆病人
深忆病人 2020-12-16 08:11

I\'ve run into a problem in a project I\'m working on: some of the string values in a specific SQL Server 2008 table column contain Unicode characters. For example, instead

3条回答
  •  没有蜡笔的小新
    2020-12-16 08:26

    One way you can do it is to see which rows no longer equal themselves when converted to a datatype that doesn't support unicode.

    CREATE TABLE myStrings (
        string nvarchar(max) not null
    )
    
    INSERT INTO myStrings (string)
    SELECT 'This is not unicode' union all
    SELECT 'This has '+nchar(500)+' unicode' union all
    SELECT 'This also does not have unicode' union all
    SELECT 'This has lots of unicode '+nchar(600)+nchar(700)+nchar(800)+'!'
    
    SELECT cast(string as varchar)
    FROM myStrings
    
    SELECT *
    FROM myStrings
    WHERE cast(cast(string as varchar(max)) as nvarchar(max)) <> string
    

提交回复
热议问题