N Prefix for Unicode strings in SQL Server

When you pass Unicode data to data types that support Unicode data (nchar, nvarchar and ntext) in SQL Server you must prefix the Unicode string with N to avoid loss of character data for characters in the Unicode string that do not exist in the SQL Server database's code page.

Here are a few examples:

DECLARE @my_unicode_with_N nchar(1)
SET @my_unicode_with_N = N'愛'
SELECT @my_unicode_with_N AS 'With N Prefix'

DECLARE @my_unicode_without_N nchar(1)
SET @my_unicode_without_N = '愛'
SELECT @my_unicode_without_N AS 'Without N Prefix'

N_prefix_unicode_example_1

DECLARE @my_unicode_with_N nchar(1)
SET @my_unicode_with_N = N'ñ'
SELECT @my_unicode_with_N AS 'With N Prefix'

DECLARE @my_unicode_without_N nchar(1)
SET @my_unicode_without_N = 'ñ'
SELECT @my_unicode_without_N AS 'Without N Prefix'

N_prefix_unicode_example_2

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">