2

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

INSERT INTO table VALUES ("玄真") 

It shows ¿¿ in the table. What should I do?

I'm using SQL Developer.

Justin Holze
  • 181
  • 1
  • 2
  • 11
  • @a_horse_with_no_name the Font preference in SQLDev controls both the grids for data display AND the code editors...so if it looks right in the INSERT statement in the worksheet, it's good for the grids – thatjeffsmith Apr 19 '18 at 13:25

3 Answers3

5

Use single quotes, rather than double quotes, to create a text literal and for a NVARCHAR2/NCHAR text literal you need to prefix it with N

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE table_name ( value NVARCHAR2(20) );

INSERT INTO table_name VALUES (N'玄真');

Query 1:

SELECT * FROM table_name

Results:

| VALUE |
|-------|
|    玄真 |
MT0
  • 143,790
  • 11
  • 59
  • 117
1

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;

enter image description here

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.

enter image description here

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         
thatjeffsmith
  • 20,522
  • 6
  • 37
  • 120
-3

First of all, you should to establish the Chinese character encoding on your Database, for example

UTF-8, Chinese_Hong_Kong_Stroke_90_BIN, Chinese_PRC_90_BIN, Chinese_Simplified_Pinyin_100_BIN ... 

I show you an example with SQL Server 2008 (Management Studio) that incorporates all of this Collations, however, you can find the same characters encodings in other Databases (MySQL, SQLite, MongoDB, MariaDB...).

  1. Create Database with Chinese_PRC_90_BIN, but you can choose other Coallition:

    Select a Page (Left Header) Options > Collation > Choose the Collation

enter image description here

  1. Create a Table with the same Collation:

  2. Execute the Insert Statement

INSERT INTO ChineseTable VALUES ('玄真'); enter image description here

R. García
  • 815
  • 9
  • 20
  • 1
    Justin is using Oracle, not SQL Server –  Apr 19 '18 at 09:05
  • I know but it's the same procedure... @a_horse_with_no_name – R. García Apr 19 '18 at 09:09
  • 1
    No, the procedure to change the encoding in Oracle is **completely** different (in fact you can't even change the encoding once a database instance is created). Even "creating a database" means something **completely** different in Oracle. Nothing in your answer is applicable to Oracle –  Apr 19 '18 at 09:10
  • Of course, you can not change the character encoding of your database when it was created, however, I do not agree, because I have worked with SQL Developer (Oracle) and yes, you can perfectly change the character encoding before creating it in the same way that SQL Server with different interface **obviously**. – R. García Apr 19 '18 at 09:23
  • Let's say I have to use the same database. – Justin Holze Apr 19 '18 at 09:28
  • Look this documentation [link](https://documentation.red-gate.com/sco3/deploying-the-data-sources/selecting-encoding-settings) – R. García Apr 19 '18 at 09:32