Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Difference in string comparisons between varchar and nvarchar

$
0
0

These statements were executed on SQL Server 2008 R2 Enterprise.  Collation is the default SQL_Latin1_General_CP1_CI_AS.

Any ideas why the difference in behavior?

DECLARE @StringWithCharZero VARCHAR(100), @StringWithoutCharZero VARCHAR(100);
SELECT @StringWithCharZero = CHAR(0) + 'TEST', @StringWithoutCharZero = 'TEST';

IF @StringWithCharZero = @StringWithoutCharZero
BEGIN
      PRINT 'VARCHAR EQUAL';
      select ASCII(LEFT(@StringWithCharZero, 1)), ASCII(LEFT(@StringWithoutCharZero, 1));
END
ELSE
BEGIN
      PRINT 'VARCHAR NOT EQUAL';
      select ASCII(LEFT(@StringWithCharZero, 1)), ASCII(LEFT(@StringWithoutCharZero, 1));
END;

GO
DECLARE @StringWithCharZero NVARCHAR(100), @StringWithoutCharZero NVARCHAR(100);
SELECT @StringWithCharZero = NCHAR(0) + N'TEST', @StringWithoutCharZero = N'TEST';

IF @StringWithCharZero = @StringWithoutCharZero
BEGIN
      PRINT 'NVARCHAR EQUAL';
      select UNICODE(LEFT(@StringWithCharZero, 1)), UNICODE(LEFT(@StringWithoutCharZero, 1));
END
ELSE
BEGIN
      PRINT 'NVARCHAR NOT EQUAL';
      select UNICODE(LEFT(@StringWithCharZero, 1)), UNICODE(LEFT(@StringWithoutCharZero, 1));
END;
GO


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>