Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does N' stands for in a SQL script ? (the one used before characters in insert script)

I generated an sql script like this,

INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
VALUES (1, N'Dave', N'ESD157', N'FD080', 7)

I wonder whats that N' exactly mean and whats its purpose here.

NOTE: By searching for the answer all i can get is that N' is a prefix for National language standard and its for using unicode data. But honestly i am not able to get a clear idea about the exact operation of N' here. I'd appreciate your help and please make it in more of an understandable way. Thanks in advance.

like image 928
Ebenezar John Paul Avatar asked Jan 16 '13 07:01

Ebenezar John Paul


People also ask

What is N in insert query?

The purpose of using N' in front of the nchar values in the insert statement is just to keep the unicode characters ( Characters that are used in other parts of the world apart from UK.

What is N in dynamic SQL?

Dynamic SQL – Simple Examples The sp_executesql procedure takes the SQL string as a parameter and executes it. Since it's the Unicode string we use the N prefix. The next thing we'll do is to build a query using variables (parameters). An example of such a query is given below.

How do I insert N rows in SQL?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.


4 Answers

N is used to specify a unicode string.

Here's a good discussion: Why do some SQL strings have an 'N' prefix?

In your example N prefix is not required because ASCII characters (with value less than 128) map directly to unicode. However, if you wanted to insert a name that was not ASCII then the N prefix would be required.

INSERT [dbo].[TableName] ([Sno], [Name], [EmployeeId], [ProjectId], [Experience]) 
VALUES (1, N'Wāhi', 'ESD157', 'FD080', 7)
like image 191
Richard Schneider Avatar answered Oct 19 '22 10:10

Richard Schneider


The "N" prefix stands for National Language in the SQL-92 standard, and is used for representing unicode characters.

Any time you pass Unicode data to SQL Server you must prefix the Unicode string with N.

It is used when the type is from NVARCHAR, NCHAR or NTEXT.

For more info refer to this: Why do some SQL strings have an 'N' prefix?

like image 24
Vishal Suthar Avatar answered Oct 19 '22 09:10

Vishal Suthar


'abcd' is a literal for a [var]char string (or maybe text, but varchar(max) would be more common now) - occupying 4 bytes memory, and using whatever code-page the SQL server is configured for. N'abcd' is a literal for a n[var]char string (or maybe ntext, but nvarchar(max) would be preferable), occupying 8 bytes of memory using UTF-16. This allows for full international usage, and frankly n[var]char should probably be the default in most systems.

like image 6
Marc Gravell Avatar answered Oct 19 '22 08:10

Marc Gravell


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.

  • SOURCE
like image 2
John Woo Avatar answered Oct 19 '22 08:10

John Woo