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.
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.
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.
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.
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)
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?
'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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With