加上 n 代表存入数据库时以 unicode 格式存储。n'string' 表示string是个unicode字符串unicode 字符串的格式与普通字符串相似,但它前面有一个 n 标识符(n 代表 sql-92 标准中的国际语言 (national language))。n 前缀必须是大写字母。例如,'michél' 是
加上 n 代表存入数据库时以 unicode 格式存储。n'string' 表示string是个unicode字符串unicode 字符串的格式与普通字符串相似,但它前面有一个 n 标识符(n 代表 sql-92 标准中的国际语言 (national language))。n 前缀必须是大写字母。例如,'michél' 是字符串常量而 n'michél' 则是 unicode 常量。unicode 常量被解释为 unicode 数据,并且不使用代码页进行计算。unicode 常量确实有排序规则,主要用于控制比较和区分大小写。为 unicode 常量指派当前数据库的默认排序规则,除非使用 collate 子句为其指定了排序规则。unicode 数据中的每个字符都使用两个字节进行存储,而字符数据中的每个字符则都使用一个字节进行存储。有关更多信息,请参见使用 unicode 数据。unicode 字符串常量支持增强的排序规则。nchar根据 unicode 标准所进行的定义,用给定整数代码返回 unicode 字符。语法nchar ( integer_expression ) 参数integer_expression介于 0 与 65535 之间的所有正整数。如果指定了超出此范围的值,将返回 null。返回类型nchar(1)示例a. 使用 nchar 和 unicode下面的示例使用 unicode 和 nchar 函数打印字符串 k?enhavn 第二个字符的 unicode 值和 nchar(unicode 字符),并打印实际的第二个字符?。declare @nstring nchar(8)set @nstring = n'k?benhavn'select unicode(substring(@nstring, 2, 1)), nchar(unicode(substring(@nstring, 2, 1)))go下面是结果集:----------- - 248 ?(1 row(s) affected)b. 使用 substring、unicode、convert 和 nchar下面的示例使用 substring、unicode、convert 和 nchar 函数打印字符串 k?enhavn 的字符数、unicode 字符以及每个字符的 unicode 值。declare @position int, @nstring nchar(9)set @position = 1set @nstring = n'k?benhavn'print 'character #' + ' ' + 'unicode character' + ' ' + 'unicode value'while @position <= datalength(@nstring) begin select @position, nchar(unicode(substring(@nstring, @position, 1))), convert(nchar(17), substring(@nstring, @position, 1)), unicode(substring(@nstring, @position, 1)) select @position = @position + 1 endgo下面是结果集:character # unicode character unicode value ----------- ----------------- ----------- 1 k 75 ----------- ----------------- ----------- 2 ? 248 ----------- ----------------- ----------- 3 b 98 ----------- ----------------- ----------- 4 e 101 ----------- ----------------- ----------- 5 n 110 ----------- ----------------- ----------- 6 h 104 ----------- ----------------- ----------- 7 a 97 ----------- ----------------- ----------- 8 v 118 ----------- ----------------- ----------- 9 n 110 ----------- ----------------- ----------- 10 (null) (null) ----------- ----------------- ----------- 11 (null) (null) ----------- ----------------- ----------- 12 (null) (null) ----------- ----------------- ----------- 13 (null) (null) ----------- ----------------- ----------- 14 (null) (null) ----------- ----------------- ----------- 15 (null) (null) ----------- ----------------- ----------- 16 (null) (null) ----------- ----------------- ----------- 17 (null) (null) ----------- ----------------- ----------- 18 (null) (null)