Sunday 26 February 2017

Use of prefix N for literal strings in Sql Server

When dealing with Unicode string constants in SQL Server you must precede all Unicode strings with a capital letter N. The "N" prefixes stands for National Language in the SQL-92 standard, and must be uppercase. If you do not prefix a Unicode string constant with N, SQL Server will convert it to the non-Unicode code page of the current database before it uses the string.

The Prefix N conveys to the Sql Server that following literal string is of Unicode type. While storing Unicode (i.e. Japanese, Korean, Chinese etc) Characters in NChar, NVarchar or NText columns or variables we need to prefix the literal strings by letter N.

See the example

Creating a table
Create Table MultiLanguage (Languageid int,
LanguageName varchar(10), Language nvarchar(100))

Now I am inserting some records without using prefix N
Insert into MultiLanguage values (1, 'Hindi', 'मेरा नाम बागेश कुमार सिंह है')
Insert into MultiLanguage values (2, 'Urdu', 'میرا نام باگےش کمار سنگھ ہے')
Insert into MultiLanguage values (3, 'English', 'My Name is Bagesh kumar singh')
Insert into MultiLanguage values (4, 'Gujrati', 'મારું નામ કુમાર સિંહ Bagesh છે')
Insert into MultiLanguage values (5, 'Malayalam','എന്റെ പേര് കുമാർ സിംഗ് Bagesh ആണ്')
Insert into MultiLanguage values (6, 'Marathi', 'माझे नाव कुमार सिंग Bagesh आहे')
Insert into MultiLanguage values (7, 'punjabi', 'ਮੇਰਾ ਨਾਮ ਕੁਮਾਰ ਸਿੰਘ Bagesh ਹੈ')
Insert into MultiLanguage values (8, 'Tamil', 'என் பெயர் குமார் சிங் Bagesh ஆகும்')
Insert into MultiLanguage values (9, 'Telagu','నా పేరు కుమార్ సింగ్ Bagesh ఉంది')
See the output
  


I mean Unicode string show?? ? Here we are not getting the correct value.
Inserting the value using N prefix

Insert into MultiLanguage values (1, 'Hindi', N'मेरा नाम बागेश कुमार सिंह है')
Insert into MultiLanguage values (2, 'Urdu', N'میرا نام باگےش کمار سنگھ ہے')
Insert into MultiLanguage values (3, 'English', N'My Name is Bagesh kumar singh')
Insert into MultiLanguage values (4, 'Gujrati', N'મારું નામ કુમાર સિંહ Bagesh છે')
Insert into MultiLanguage values (5, 'Malayalam', N'എന്റെ പേര് കുമാർ സിംഗ് Bagesh ആണ്')
Insert into MultiLanguage values (6, 'Marathi', N'माझे नाव कुमार सिंग Bagesh आहे')
Insert into MultiLanguage values (7, 'punjabi', N'ਮੇਰਾ ਨਾਮ ਕੁਮਾਰ ਸਿੰਘ Bagesh ਹੈ')
Insert into MultiLanguage values (8, 'Tamil', N'என் பெயர் குமார் சிங் Bagesh ஆகும்')
Insert into MultiLanguage values (9, 'Telagu', N'నా పేరు కుమార్ సింగ్ Bagesh ఉంది')
See the output
  


Thanks!

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts