patternsqlCritical
Write differences between varchar and nvarchar
Viewed 0 times
nvarcharvarcharwritedifferencesbetweenand
Problem
Currently in our SQL Server 2012 database, we're using
My question is are there any differences in how SQL Server writes to
Edit:
Not sure if this helps, but the columns don't have indexes, f/k, or constraints on them.
varchar, and we'd like to change that nvarchar. I've generated a script to do that.My question is are there any differences in how SQL Server writes to
varchar columns vs. nvarchar columns? We have a number of backend procedures that I'm concerned about.Edit:
Not sure if this helps, but the columns don't have indexes, f/k, or constraints on them.
Solution
You need to be sure that you prefix Unicode string literals with an N prefix. For example these will work differently if the underlying data type is
Results:
For those on mobile devices or decrepit browsers that show box characters instead of actual Unicode characters, this is what it looks like:
NVARCHAR:CREATE TABLE dbo.t(c NVARCHAR(32));
INSERT dbo.t(c) SELECT 'រៀន';
INSERT dbo.t(c) SELECT 'នរៀ';
INSERT dbo.t(c) SELECT N'រៀន';
SELECT c FROM dbo.t;
SELECT c FROM dbo.t WHERE c = 'រៀន';
SELECT c FROM dbo.t WHERE c = N'រៀន';Results:
c
----
??? -- not stored correctly
??? -- not stored correctly
រៀន -- stored correctly!
c
----
???
??? -- probably not expected, however all Unicode characters have been changed to ?
c
----
រៀនFor those on mobile devices or decrepit browsers that show box characters instead of actual Unicode characters, this is what it looks like:
Code Snippets
CREATE TABLE dbo.t(c NVARCHAR(32));
INSERT dbo.t(c) SELECT 'រៀន';
INSERT dbo.t(c) SELECT 'នរៀ';
INSERT dbo.t(c) SELECT N'រៀន';
SELECT c FROM dbo.t;
SELECT c FROM dbo.t WHERE c = 'រៀន';
SELECT c FROM dbo.t WHERE c = N'រៀន';c
----
??? -- not stored correctly
??? -- not stored correctly
រៀន -- stored correctly!
c
----
???
??? -- probably not expected, however all Unicode characters have been changed to ?
c
----
រៀនContext
StackExchange Database Administrators Q#36081, answer score: 51
Revisions (0)
No revisions yet.