Why do some SQL strings have an 'n' prefix?

Source: Internet
Author: User

You may have seen Transact-SQL code that passes strings around using an n prefix. this denotes that the subsequent string is in Unicode (the N actually stands for national language character set ). which means that you are passing an nchar, nvarchar or ntext value, as opposed to Char, varchar or text. see article #2354 for a comparison of these data types.
Unicode is typically used in database applications which are designed to facilitate code pages which extend beyond the English and Western Europe code pages (Erland Sommarskog, a native of Sweden, refers to this set as "Germanic and Romance ages"), for example Chinese. unicode is designed so that extended character sets can still "fit" into database columns. what this means is that Unicode character data types are limited to half the space, because each byte actually takes two bytes (Unicode is sometimes referred to as "double-wide "). for more information on Unicode, see Unicode.org. note that there are encoded encoding schemes in the Unicode standard, but SQL Server only supports one: UTF-16.
While using Unicode is a design choice you can make in building your own applications, some facilities in SQL server require it. One example is sp_executeSQL. If you try the following:

EXEC sp_ExecuteSQL 'select 1'

You will get this error:

Server: Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1
Procedure expects parameter '@ statement' of type 'ntext/nchar/nvarchar '.

You can get around this in two ways:

-- (A) using the N prefix
EXEC sp_ExecuteSQL N 'select 1'
-- (B) using a variable
DECLARE @ SQL NVARCHAR (100)
SET @ SQL = n' SELECT 1'
EXEC sp_ExecuteSQL @ SQL

Note that implicit conversion makes the N prefix optional in case (B); however, for legibility and consistency, you shoshould always use the prefix when defining Unicode strings. one reason is that leaving it off can actually change your data if it contains Unicode characters (losing the additional information), as in the following example:

DECLARE @ n NVARCHAR (10)
SET @ n = 'A'
PRINT @ n
SET @ n = n''
PRINT @ n

The first assignment, which didn't use the N prefix, gets printed as a regular. only the second maintains the character that was actually supposed to be represented. as you can imagine, if you are intending to support data entry in foreign ages and code pages, you will likely need to test for Unicode support (making sure that such columns support Unicode, and that data won't be lost when passed into stored procedures, functions, etc .). note that your application will need to handle Unicode as well; for example, when you try to print this character from ASP...

<%
Response. Write ("")
%>

... It actually prints out the string aa. (This result might depend on your codepage and regional settings .) so you might consider translating your data into its ASCII equivalent, e.g. a = & amp; #0257 ;.
Another reason you want to avoid implicit conversion is that there are some potentially serious performance issues. Consider the following quite simple repro:

Use tempdb
Go
Create Table
(
B varchar (3 ),
C nvarchar (3)
)
Create index B on A (B)
Create index c on a (c)
Go
Set nocount on
Insert a select 'foo', N 'foo'
Insert a select 'bar', N 'bar'
Declare
@ B varchar (3 ),
@ C nvarchar (3)
Select
@ B = 'foo ',
@ C = n'foo'
Select * From A Where B = @ B
Select * From A Where B = @ C
SELECT * FROM a WHERE c = @ B
SELECT * FROM a WHERE c = @ c
SELECT * FROM a WHERE B LIKE @ B
SELECT * FROM a WHERE B LIKE @ c
SELECT * FROM a WHERE c LIKE @ B
SELECT * FROM a WHERE c LIKE @ c
Drop table

Paste the code into Query Analyzer, turn execution plan on, and let her rip. You'll observe the following breakdown of percentage of work (roughly, depending on your hardware ):

VARCHAR = VARCHAR
4.48%

VARCHAR = NVARCHAR
13.31%

NVARCHAR = VARCHAR
4.48%

NVARCHAR = NVARCHAR
4.48%

VARCHAR LIKE VARCHAR
4.48%

VARCHAR LIKE NVARCHAR
13.31%

NVARCHAR LIKE VARCHAR
4.48%

NVARCHAR LIKE NVARCHAR
4.48%

Now, that's not the whole story; we all know that there are using other factors, such as I/O, that will impact the actual time each portion of the query takes. the key is that implicit conversion * can * cause a table scan instead of an index seek, and on larger tables this can really hurt. while it's important to understand why this happens and in which scenarios, my recommendation is to match your character-based datatypes as explicitly as possible.
One other thing to watch out for: Your database may be using Unicode without your knowledge. if you upsize from access to SQL Server, for example, character-based text columns might be translated to Unicode (I believe this is a catch-all technique; in case access was storing Unicode strings, or if you might be storing Unicode strings later, you won't lose data or require changes ). I think the access upsizing tools shoshould be updated to force a conscous choice, so that you aren't wasting space for nothing, and so that you know that you made a demo-at all.
For a more thorough discussion of Unicode and the N prefix, please see kb #239530, this msdn article, and this Google thread.
In other RDBMS platforms, or in the ANSI and/or ISO specifications, you might see prefixes other than N being used against values. (current versions of SQL Server only support Unicode .) here are the additional monikers I am aware:

B
This is used to denote a BINARY string expressed in bits (0 and 1 only)

X
This is used to denote a BINARY string expressed in hexadecimal (0-> F)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.