I have seen prefix N in some insert T-SQL queries. Many people have used N
before inserting the value in a table.
I searched, but I was not able to understand what is the purpose of including the N
before inserting any strings into the table.
INSERT INTO Personnel.Employees
VALUES(N'29730', N'Philippe', N'Horsford', 20.05, 1),
let me tell you what an annoying thing happened with the thing N'' prefix . and iwasn't able to fix 2 days long.
My db collation is SQL_Latin1_General_CP1_CI_AS.
there is a table. column MyCol1 is Nvarchar
but this query fails to match Exact Value That Exist.
using prefix N'' fixes it
why? because latin1_general doesnt have big dotted İ thats why it fails i suppose.
It's declaring the string as
nvarchar
data type, rather thanvarchar
To quote from Microsoft:
If you want to know the difference between these two data types, see this SO post:
What is the difference between varchar and nvarchar?
Assuming the value is nvarchar type for that only we are using N''
Assume your where clause is like this:
If the NAME column is of any type other than nvarchar or nchar, then you should not specify the N prefix. However, if the NAME column is of type nvarchar or nchar, then if you do not specify the N prefix, then 'JON' is treated as non-unicode. This means the data type of NAME column and string 'JON' are different and so SQL Server implicitly converts one operand’s type to the other. If the SQL Server converts the literal’s type to the column’s type then there is no issue, but if it does the other way then performance will get hurt because the column's index (if available) wont be used.
If the column is of type nvarchar or nchar, then always use the prefix N while specifying the character string in the WHERE/UPDATE/INSERT clause. If you do not do this and one of the characters in your string is unicode (like international characters - example - ā) then it will fail or suffer data corruption.