UTF-8 characters get saved as ?? on insert, but ge

2019-09-10 22:46发布

问题:

I have a table on MS SQLServer with an nVarchar column. I am saving a UTF-8 character using an insert statement. It gets saved as ???. If I update the same column using the same value via an update statement, it gets saved correctly.

Any hint on what would be the issue here? The collation used is : SQL_Latin1_General_CP1_CI_AS

回答1:

Show your insert statement. There is - quite probably - an N missing:

DECLARE @v NVARCHAR(100)='Some Hindi from Wikipedia मानक हिन्दी';
SELECT @v;

Result: Some Hindi from Wikipedia ???? ??????

SET @v=N'Some Hindi from Wikipedia मानक हिन्दी';
SELECT @v;

Result: Some Hindi from Wikipedia मानक हिन्दी

The N in front of the string literal tells SQL-Server to interpret the content as unicode (to be exact: as ucs-2). Otherwise it will be treated as a 1-byte-encoded extended ASCII, which is not able to deal with all characters...