zoukankan      html  css  js  c++  java
  • 字符前面的N

    1.建立一个测试表

    CREATE table myTestChar
    (
    c1 int not null identity primary key,
    c2 char(8)
    )

    插入两条语句:

    INSERT into myTestChar VALUES('中国')

     INSERT into myTestChar VALUES(N'中国')

    select * from myTestChar

    c1 c2
    1 ??     
    2 ??     

    返回的都是乱码,这是因为汉字和字符采用的是不同的编码方式,不论是否强制的采用unicode编码方式都是乱码。字符无法存储中文。

    (  有的数据库版本不会出现这种现象)

    2.给该表添加一个字段,是nchar类型的

    ALTER table mytestChar
    add c3 nchar(8)

    插入两条记录:

    INSERT into myTestChar VALUES(N'中国','中国')

    INSERT into myTestChar VALUES(N'中国',N'中国')

    返回结果:

    c1 c2 c3
    1 ??       NULL
    2 ??       NULL
    3 ??       ??     
    4 ??       中国     

    INSERT into myTestChar VALUES(N'中国','china')
    INSERT into myTestChar VALUES(N'中国',N'china')

     返回结果:

    c1 c2 c3
    1 ??       NULL
    2 ??       NULL
    3 ??       ??     
    4 ??       中国     
    5 ??       china  
    6 ??       china  

     结论:存储中国字的时候需要强制编码下。因为存储一个中国字需要两个字节。

    INSERT into myTestChar VALUES(N'中国中国中国','china')

    执行上面语言会出现:

    将截断字符串或二进制数据。
    语句已终止。

    这个因为6个字需要12char来存储。

    INSERT into myTestChar VALUES(N'中国中')

    则执行成功。

  • 相关阅读:
    Groovy Closure & Action
    Groovy A simple DSL based on groovy
    Groovy 一些小细节
    Android 那些年踩过的坑
    Android Startup
    Android 开发最佳实践
    Android 开发经验-容易忽略的问题
    Android 开发经验-Fragment相关
    AQTime + Delphi
    关于ANSI,unicode与utf-8的区别
  • 原文地址:https://www.cnblogs.com/363546828/p/3048630.html
Copyright © 2011-2022 走看看