t-sql 中 nvarchar 变量的强制转换排序规则

发布于 2024-08-08 15:37:57 字数 519 浏览 2 评论 0原文

我需要更改 nvarchar 变量的排序规则。 通过文档

(...) 3. 可以指定COLLATE子句 在几个层面上。这些包括 以下:

转换一个的排序规则 表达。您可以使用整理 应用字符表达式的子句 到一定的排序规则。特点 文字和变量已赋值 当前的默认排序规则 数据库。列参考文献是 分配的定义排序规则 专栏。为了整理一份 表达式,请参阅排序规则优先级 (Transact-SQL)。

但是,我无法找出使用 CAST()、CONVERT() 或使用 DECLARE 进行变量声明的正确语法。

I need to change the collation of an nvarchar variable. By documentation:

(...)
3. The COLLATE clause can be specified
at several levels. These include the
following:

Casting the collation of an
expression. You can use the COLLATE
clause to apply a character expression
to a certain collation. Character
literals and variables are assigned
the default collation of the current
database. Column references are
assigned the definition collation of
the column. For the collation of an
expression, see Collation Precedence
(Transact-SQL).

However I can't figure out the correct syntax for the usage of CAST(), CONVERT() or variable declaration with DECLARE for this purpose.

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

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

发布评论

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

评论(4

ㄖ落Θ余辉 2024-08-15 15:37:57
SELECT CAST('abc' AS varchar(5)) COLLATE French_CS_AS
SELECT CAST('abc' AS varchar(5)) COLLATE French_CS_AS
静谧 2024-08-15 15:37:57

CASTCONVERT 是多余的!

SELECT N'abc' COLLATE French_CS_AS

这是多余的,因为仅更改排序规则不会更改数据类型 NVARCHAR

CAST or CONVERT is superfluous!

SELECT N'abc' COLLATE French_CS_AS

It is superfluous because just changing the collation does not change the data type NVARCHAR.

顾北清歌寒 2024-08-15 15:37:57

如果您要在 2 和 1 字节之间更改字符编码(反之亦然),则需要 CAST 或 Convert。在这些情况下它并不是多余的。

当源列是 2 字节字符序列(nchar、nvarchar)并且要求选择投影是单字节字符(char、varchar)时,应指定强制转换和转换。在类型系统之间进行转换之前应用排序规则转换。

SELECT CAST(N'ФBC' COLLATE SQL_Latin1_General_CP1_CI_AS as varchar(10)) AS ProjectedSingleByte

If you are changing between 2 and 1 byte, or vice-ver-sa, character encodings then CAST or Convert is necessary. It is not superfluous in these cases.

When the source column is a 2 byte character sequence (nchar, nvarchar) and the selection projection is required to be a single byte character (char, varchar), you should specify the cast and convert. Apply the collation conversion before the casting between the type systems.

SELECT CAST(N'ФBC' COLLATE SQL_Latin1_General_CP1_CI_AS as varchar(10)) AS ProjectedSingleByte
千里故人稀 2024-08-15 15:37:57

如果您想比较或连接不同排序规则的两列,这可能会有所帮助。
就我而言,我必须比较两列,其中一列使用“SQL_Latin1_General_CP1_CI_AS”,另一列使用“Latin1_General_CP1_CI_AS”。

我只是在加入这两个选项时使用了这个选项。

在A.Person = B.NAME上整理database_default

If you would like to compare or join two columns of different collation this could help.
In my case I had to compare two columns with one using 'SQL_Latin1_General_CP1_CI_AS' and the other using 'Latin1_General_CP1_CI_AS'.

I simply used this option where i join these two.

on A.Person = B.NAME collate database_default

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