你直接 select name from table1 就能返回.还是想显示成n'+name?select 'N''' + name + '''' from table1
select unicode(cast(name as nvarchar)) from table1
当前数据库的排序规则是什么?是否支持中文的 ascii 编码?
我查了,支持中文ascii编码
不行。结果跟我直接写select unicode(name) from table1效果是一样滴 只返回了第一个字节的unicode码,没有返回一个中文字符的unicode码我用以下测试代码来返回一个中文字符的unicode就可以: declare @str1 nchar(12) set @str1=N'中' print unicode(@str1)返回了20013(我查了,确实是“中”这个字的unicode码)所以我想在select语句中针对name这个字段也这么弄,但老是报错。
name 列的排序规则是什么?-- 检查当前数据库的排序规则 select collation_name from sys.databases where database_id=db_id() -- 检查表中 name 列的排序规则 select collation_name from sys.columns where [object_id]=object_id('table1') and [name]='name'
-- 在排序规则为 Chinese_PRC_CI_AS 的数据下 declare @c varchar(10); set @c='中文'; select unicode(@c); -- 20013if object_id('tab') is not null drop table tab; create table tab(c varchar(10) collate Chinese_PRC_CI_AS); insert into tab select '中文'; select unicode(c) from tab; -- 20013if object_id('tab') is not null drop table tab; create table tab(c varchar(10) collate SQL_Latin1_General_CP1_CI_AS); insert into tab select convert(varbinary(10),'中文'); select unicode(c) from tab; -- 214 select convert(varchar(10),convert(varbinary(10),c)) from tab -- 中文 select unicode(convert(varchar(10),convert(varbinary(10),c))) from tab -- 20013
-----------
65(所影响的行数为 1 行)要不然用ASCII试试
select name from table1
就能返回.还是想显示成n'+name?select 'N''' + name + '''' from table1
select unicode(cast(name as nvarchar)) from table1
我查了,支持中文ascii编码
只返回了第一个字节的unicode码,没有返回一个中文字符的unicode码我用以下测试代码来返回一个中文字符的unicode就可以:
declare @str1 nchar(12)
set @str1=N'中'
print unicode(@str1)返回了20013(我查了,确实是“中”这个字的unicode码)所以我想在select语句中针对name这个字段也这么弄,但老是报错。
select collation_name from sys.databases where database_id=db_id()
-- 检查表中 name 列的排序规则
select collation_name from sys.columns where [object_id]=object_id('table1') and [name]='name'
-- 在排序规则为 Chinese_PRC_CI_AS 的数据下
declare @c varchar(10);
set @c='中文';
select unicode(@c);
-- 20013if object_id('tab') is not null
drop table tab;
create table tab(c varchar(10) collate Chinese_PRC_CI_AS);
insert into tab select '中文';
select unicode(c) from tab;
-- 20013if object_id('tab') is not null
drop table tab;
create table tab(c varchar(10) collate SQL_Latin1_General_CP1_CI_AS);
insert into tab select convert(varbinary(10),'中文');
select unicode(c) from tab;
-- 214
select convert(varchar(10),convert(varbinary(10),c)) from tab
-- 中文
select unicode(convert(varchar(10),convert(varbinary(10),c))) from tab
-- 20013
实际情况是这样的:
该字段是char(20),排序规则是SQL_Latin1_General_CP1_CI_AS
字段内容是按照ascii存储的(不管是中文还是英文),中文就是两个ascii,英文就是一个ascii
我现在就想把中文的两个ascii合并成一个中文字符存储到另外一个表里面去。