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.

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.