为什么sql server在NVarchar字段中存储问号字符而不是日文字符?

Sti*_*imy 28 sql-server

我正在尝试将日语字符存储在SQL Server 2000数据库的nvarchar字段中.

当我运行更新语句时:

update blah 
set address = N'????????'
where key_ID = 1
Run Code Online (Sandbox Code Playgroud)

从SQL Server Management Studio,然后运行一个select语句我只看到返回到结果窗口的问号.我在网页上看到了相同的问号,它看着数据库.

这似乎是存储正确数据的问题吗?谁能告诉我我需要做些什么呢?

mik*_*ika 96

根据您的示例,这不是一个正确的答案,但我看到的最常见的原因是字符串文字不需要unicode N前缀.

所以,而不是

set address = N'????????'
Run Code Online (Sandbox Code Playgroud)

我会尝试写入没有unicode前缀的nvarchar字段

set address = '????????'
Run Code Online (Sandbox Code Playgroud)

另请参见: Transact-SQL查询中字符串前的N前缀

  • 你我的朋友是个天才! (4认同)
  • 在我的情况下,有文本而不是ntext (2认同)
  • 我正在添加罗马尼亚文字,因此必须在字符串前加上n`N''`。因此是相反的。我尝试的第一种方法是不使用N,但这不起作用。谢谢。 (2认同)

Dee*_*hri 14

在DB nvarchar字段中存储时使用印度语字符时,我遇到了同样的问题.然后我浏览了这篇微软文章 -

http://support.microsoft.com/kb/239530

我遵循了这一点,我的unicode问题得到了解决.在本文中,他们说 - 当您在SQL Server中处理Unicode字符串常量时,必须在所有Unicode字符串前面加上前缀N

SQL Server Unicode支持

SQL Server Unicode数据类型支持UCS-2编码.Unicode数据类型使用每个字符的两个字节而不是一个字节来存储字符数据.两个字节中有65,536种不同的位模式,因此Unicode可以使用一组标准位模式来编码所有语言中的每个字符,包括具有大量字符的中文等语言.

在SQL Server中,支持Unicode数据的数据类型是:

nchar
nvarchar
nvarchar(max) – new in SQL Server 2005
ntext
Run Code Online (Sandbox Code Playgroud)

使用nchar,nvarchar,nvarchar(max)和ntext分别与char,varchar,varchar(max)和text相同,除了:

- Unicode supports a wider range of characters.
- More space is needed to store Unicode characters.
- The maximum size of nchar and nvarchar columns is 4,000 characters, not 8,000     characters like char and varchar.
- Unicode constants are specified with a leading N, for example, N'A Unicode string'
Run Code Online (Sandbox Code Playgroud)

适用于

Microsoft SQL Server 7.0 Standard Edition
Microsoft SQL Server 2000 Standard Edition
Microsoft SQL Server 2005 Standard Edition
Microsoft SQL Server 2005 Express Edition
Microsoft SQL Server 2005 Developer Edition
Microsoft SQL Server 2005 Enterprise Edition
Microsoft SQL Server 2005 Workgroup Edition
Run Code Online (Sandbox Code Playgroud)


Par*_*esh 7

代码绝对没问题。您可以将带有前缀 N 的 unicode 字符串插入声明为 NVARCHAR 的字段中。那么您可以检查 Address 是否是 NVARCHAR 列。在 SQL Server 2008R2 中测试了以下代码并且它有效。

\n\n
update blah \nset address = N\'\xe3\x82\xb9\xe3\x82\xbf\xe3\x83\xb3\xe3\x83\x80\xe3\x83\xbc\xe3\x83\x89\xe3\x83\x81\xe3\x83\xa3\'\nwhere key_ID = 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n


cjk*_*cjk 5

您需要从数据库,数据访问和表示层中检查处理此数据的所有代码的全球化设置。这包括SSMS。

(您还需要弄清楚正在使用的版本,2003不存在...)

  • 我不明白为什么这个答案有(任何)票...数据库(或对象)排序规则与您可以在数据库中存储和不能存储的内容无关!它用于索引,排序和常规字符串比较。也就是说,“您需要检查来自数据库,数据访问和表示层的所有处理此数据的代码的全球化设置”部分是正确的,并且问题可能出在其中。 (7认同)

小智 5

您需要在字符串值之前写 N。egINSERT INTO LabelManagement (KeyValue) VALUES (N'????'); 在这里,我用日语存储值,并且在字符串字符之前添加了 N。我正在使用 Sql server 2014。希望你找到解决方案。享受。