1

I'm trying to execute the below-mentioned query in MSSQL.

insert into STUDENT (STUDENT_NAME, STUDENT_MARKS, SID) values ('索引', 10, 2)

STUDENTNAME column is of type NVARCHAR.

After execution of the above query. The student name getting inserted is: ??

I understand that the above issue is related to encoding and so I modified the above query by adding N, as shown below :

insert into STUDENT (STUDENT_NAME, STUDENT_MARKS, SID) values (N'索引', 10, 2)

The above query is running as expected and is inserting the Chinese characters successfully.

Question: I'm using JDBC to execute this insert query, So do I need to proceed all the values with N as shown above. Or there is some other setting at the driver level to fix this?

1 Answers1

2

I found this on a Microsoft article,

Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.

N denotes that the string is in Unicode (the N actually stands for National language character set), which means that you are passing an NCHAR, NVARCHAR or NTEXT value, as opposed to CHAR, VARCHAR or TEXT.

Please follow this link post:

Difference between varchar and nvarchar?

Further to your question, if you are using INSERT queries to insert the records in the table, then YES you have to add an N before every string value.

Babu Swami
  • 798
  • 3
  • 10