mssql中文字符处理(字符集编码和排序规则)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mssql中文字符处理(字符集编码和排序规则),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1875字,纯文字阅读大概需要3分钟。
内容图文
mssql 中文字符处理(字符集编码和排序规则)sqlserver中文处理涉及到字符集编码和排序规则,是个非常纠结的问题。
sql code
--ascii字符
select n,x=cast(n as binary(2)),u=nchar(n) from nums where n between 32 and 126
--unicode中文字符
select n,x=cast(n as binary(2)),u=nchar(n) from nums where n between 19968 and 40869
19968 0x4e00 一
40869 0x9fa5
--以下两个条件用来判断字符串是否包含汉字
like n'%[-]%' collate chinese_prc_ci_as
like n'%[一-]%' collate chinese_prc_bin
--这是因为在以上两种不同的排序规则下,汉字的排列顺序是不同的。
--中文全角标点符号
select n,x=cast(n as binary(2)),uq=nchar(n),ub=nchar(n-65248) from nums where n between 65281 and 65374
select nchar(12288),nchar(32)
65281 0xff01 ! !
65374 0xff5e ~ ~
--以下条件用来判断字符串是否包含全角标点
like n'%[!-~]%' collate chinese_prc_bin
全角半角标点的转换:
sql code
--full2half
create function [dbo].[full2half](
@string nvarchar(max)
)
returns nvarchar(max)
as
/*
全角(fullwidth)转换为半角(halfwidth)
*/
begin
declare @chr nchar(1)
declare @i int
set @string = replace(@string,n' ',n' ')
set @i = patindex(n'%[!-~]%' collate latin1_general_bin,@string)
while @i > 0
begin
set @chr = substring(@string,@i,1)
set @string = replace(@string,@chr,nchar(unicode(@chr)-65248))
set @i = patindex(n'%[!-~]%' collate latin1_general_bin,@string)
end
return @string
end
go
create function [dbo].[half2full](
@string nvarchar(max)
)
returns nvarchar(max)
as
/*
半角(halfwidth)转换为全角(fullwidth)
*/
begin
declare @chr nchar(1)
declare @i int
set @string = replace(@string,n' ',n' ')
set @i = patindex(n'%[!-~]%' collate latin1_general_bin,@string)
while @i > 0
begin
set @chr = substring(@string,@i,1)
set @string = replace(@string,@chr,nchar(unicode(@chr)+65248))
set @i = patindex(n'%[!-~]%' collate latin1_general_bin,@string)
end
return @string
end
go
内容总结
以上是互联网集市为您收集整理的mssql中文字符处理(字符集编码和排序规则)全部内容,希望文章能够帮你解决mssql中文字符处理(字符集编码和排序规则)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。