VARCHAR/CHAR 如何在 SQL Server 中存储/呈现跨国符号?

发布于 2024-09-27 14:39:30 字数 348 浏览 2 评论 0原文

我曾经读到过,varchar (char) 用于存储每个字符 1 个字节的 ASCII 字符,而 nvarchar (varchar) 使用带有 2 个字节的 UNICODE。
但是哪个 ASCII 呢?在 SSMS 2008 R2

DECLARE @temp VARCHAR(3); --CHAR(3)   
SET @temp = 'ЮЯç'; --cyryllic + portuguese-specific letters
select @temp,datalength(@temp) 
-- results in 
-- ЮЯç  3

更新中:哎呀,结果确实是 ЮЯс,但不是 ЮЯç。谢谢,马丁

I have used to read that varchar (char) is used for storing ASCII characters with 1 bute per character while nvarchar (varchar) uses UNICODE with 2 bytes.
But which ASCII? In SSMS 2008 R2

DECLARE @temp VARCHAR(3); --CHAR(3)   
SET @temp = 'ЮЯç'; --cyryllic + portuguese-specific letters
select @temp,datalength(@temp) 
-- results in 
-- ЮЯç  3

Update: Ooops, the result was really ЮЯс but not ЮЯç. Thanks, Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-10-04 14:39:30
declare @table table
(
c1 char(4) collate Cyrillic_General_CS_AI,
c2 char(4) collate Latin1_General_100_CS_AS_WS
)

INSERT INTO @table  VALUES (N'ЮЯçæ', N'ЮЯçæ')

SELECT c1,cast(c1 as binary(4)) as c1bin, c2, cast(c2 as binary(4)) as c2bin
FROM @table

返回

c1   c1bin      c2   c2bin
---- ---------- ---- ----------
ЮЯc? 0xDEDF633F ??çæ 0x3F3FE7E6

您可以看到,根据排序规则,非 ASCII 字符可能会丢失或默默地转换为接近的等效字符。

declare @table table
(
c1 char(4) collate Cyrillic_General_CS_AI,
c2 char(4) collate Latin1_General_100_CS_AS_WS
)

INSERT INTO @table  VALUES (N'ЮЯçæ', N'ЮЯçæ')

SELECT c1,cast(c1 as binary(4)) as c1bin, c2, cast(c2 as binary(4)) as c2bin
FROM @table

Returns

c1   c1bin      c2   c2bin
---- ---------- ---- ----------
ЮЯc? 0xDEDF633F ??çæ 0x3F3FE7E6

You can see that dependant upon the collation non ASCII characters can get lost or silently converted to near equivalents.

要走就滚别墨迹 2024-10-04 14:39:30

它是 ASCII,其代码页定义了前 128 个字符 (128-255)。这是由 SQL Server 中的“排序规则”控制的,根据您使用的排序规则,您可以使用“特殊”字符的子集。

请参阅此 MSDN 页面

It's ASCII with a codepage which defines the upper 128 characters (128-255). This is controlled by the "collation" in SQL Server, and depending on the collation you use you can use a subset of "special" characters.

See this MSDN page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文