Using “N” before strings in SQL Server

When you are working with queries and commands Microsoft SQL Server or looking at documentation for arguments in core functions, often you’ll see an “N” before a quotes string, like this:

N'string content'

As you may know, the N encodes the string as Unicode (specifically, UTF-16 in SQL Server’s case). The “N” stands for “national,” since using it ensures that strings in different national languages still work.

However, if you insert data into an nvarchar column, you may notice that leaving off the “N” still works. This may be because the particular characters being used in the string have the same encoding in Unicode, or because the default collation for the database was changed to Unicode. However, if you leave out the “N,” you may have trouble later if special characters are used in the text, or if the code is ported to a database with a different collation setting.

Note that when using nvarchar, strings take twice the space that they would with varchar, because of the extra memory needed to make way for characters in all languages.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s