SQL Command to insert Chinese Letters

前端 未结 3 1869
北恋
北恋 2021-01-23 06:23

I have a database with one column of the type nvarchar. If I write

INSERT INTO table VALUES (\"玄真\") 

It shows ¿¿ in the table. W

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 06:58

    First, using NVARCHAR might not even be necessary.

    The 'N' character data types are for storing data that doesn't 'fit' in the database's defined character set. There's an auxiliary character set defined as the NCHAR Character set. It's kind of a band aid - once you create a database it can be difficult to change its character set. Moral of this story - take great care in defining the Character Set when creating your database and do not just accept the defaults.

    Here's a scenario (LiveSQL) where we're storing a Chinese string in both NVARCHAR and VARCHAR2.

    CREATE TABLE SO_CHINESE ( value1 NVARCHAR2(20), value2 varchar2(20 char));
    INSERT INTO SO_CHINESE VALUES (N'玄真', '我很高興谷歌翻譯。' )
    select * from SO_CHINESE;
    

    Note that both the character sets are in the Unicode family. Note also I told my VARCHAR2 string to hold 20 characters. That's because some characters may require up to 4 bytes to be stored. Using a definition of (20) would give you only room to store 5 of those characters.

    Let's look at the same scenario using SQL Developer and my local database.

    And to confirm the character sets:

    SQL> clear screen
    SQL> set echo on
    SQL> set sqlformat ansiconsole
    SQL> select * 
      2  from database_properties 
      3  where PROPERTY_NAME in 
      4     ('NLS_CHARACTERSET', 
      5      'NLS_NCHAR_CHARACTERSET');
    
    PROPERTY_NAME            PROPERTY_VALUE   DESCRIPTION           
    NLS_NCHAR_CHARACTERSET   AL16UTF16        NCHAR Character set   
    NLS_CHARACTERSET         AL32UTF8         Character set         
    

提交回复
热议问题