11

Like:
insert into table (col) values (N'multilingual unicode strings')

I'm using SQL Server 2008 and I already use nVarChar as the column data type.

devio
  • 36,858
  • 7
  • 80
  • 143

4 Answers4

13

You need the N'' syntax only if the string contains characters which are not inside the default code page. "Best practice" is to have N'' whenever you insert into an nvarchar or ntext column.

devio
  • 36,858
  • 7
  • 80
  • 143
  • If my codepage is UTF-8, do I still have to use the prefix N? Thanks! –  Dec 26 '08 at 05:22
3

Yes, you do if you have unicode characters in the strings.

From books online (http://msdn.microsoft.com/en-us/library/ms191313.aspx)...

"Unicode string constants that appear in code executed on the server, such as in stored procedures and triggers, must be preceded by the capital letter N. This is true even if the column being referenced is already defined as Unicode. Without the N prefix, the string is converted to the default code page of the database. This may not recognize certain characters. The requirement to use the N prefix applies to both string constants that originate on the server and those sent from the client."

Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
0

It is preferable for compatibility sake.

Vincent
  • 22,366
  • 18
  • 58
  • 61
0

Best practice is to use parameterisation in which case you don't need the N prefix.

kroiz
  • 1,722
  • 1
  • 27
  • 43
  • 3
    Depends on the context. If this is a purely TSQL Script then parameters won't help as you still need to use the `N` prefix when assigning values to the parameters. – Martin Smith Sep 25 '11 at 21:39