2013-06-26 52 views
0

爲什麼在以下腳本中,charindex在SQL Server 2008 R2中返回的值不是0?nvarchar字段中的不可見字符?

--drop table #tmp 
--go 

create table #tmp (field1 nvarchar(50)) 
go 
insert into #tmp values (N'Value 1') 
go 
insert into #tmp values (N'Value 2') 
go 
insert into #tmp values (N'Value 3') 
go 


DECLARE @i INT; 
DECLARE @c NCHAR; 
declare @nvc nvarchar(50) = N'Test Value'; 

SET @i = 128; 


WHILE @i < 256 
BEGIN 
    SET @i = @i + 1; 
    SET @c = nchar(@i); 

    if exists (select 1 from #tmp t where charindex (@c, t.field1) > 0) 
    print @c; 
END 

在我的分貝實例產生的輸出(使用歸類:SQL_Latin1_General_CP1_CI_AS):

(1 row(s) affected) 

(1 row(s) affected) 

(1 row(s) affected) 
² 
³ 
¹ 

回答

0

字符「2」和「上標2」都具有相同的unicode數字的2值,所以我會假設SQL服務器將它們視爲基於unicode字符的這個組件的等價值,而不是它看起來的樣子。