Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the prefix N in T-SQL statements and when should I use it?

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), 

What purpose does this 'N' prefix serve, and when should it be used?

like image 675
Kartik Patel Avatar asked Apr 05 '12 08:04

Kartik Patel


People also ask

Why is a varchar getting prefixed by N?

NCHAR, NVARCHAR or NTEXT are similar to CHAR, VARCHAR OR TEXT, where the N prefix represents the International Language Character Set. The N-prefixed data types indicate that the resulting string could be comprised of a Unicode character set with variable length where each character occupies 2 bytes.

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.

When to use begin and end in SQL?

BEGIN and END are used in Transact-SQL to group a set of statements into a single compound statement, so that control statements such as IF … ELSE, which affect the performance of only a single SQL statement, can affect the performance of the whole group.

What is prefix in SQL query?

Prefix enables developers to easily see what their code is doing as they write and test their code. Including SQL queries, HTTP calls, errors, logs, and much more. This makes Prefix really handy for viewing SQL queries your code is using.


2 Answers

It's declaring the string as nvarchar data type, rather than varchar

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.

To quote from Microsoft:

Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.


If you want to know the difference between these two data types, see this SO post:

What is the difference between varchar and nvarchar?

like image 97
Curtis Avatar answered Sep 23 '22 16:09

Curtis


Let me tell you an annoying thing that happened with the N' prefix - I wasn't able to fix it for two days.

My database collation is SQL_Latin1_General_CP1_CI_AS.

It has a table with a column called MyCol1. It is an Nvarchar

This query fails to match Exact Value That Exists.

SELECT TOP 1 * FROM myTable1 WHERE  MyCol1 = 'ESKİ'    // 0 result 

using prefix N'' fixes it

SELECT TOP 1 * FROM myTable1 WHERE  MyCol1 = N'ESKİ'    // 1 result - found!!!! 

Why? Because latin1_general doesn't have big dotted İ that's why it fails I suppose.

like image 23
bh_earth0 Avatar answered Sep 24 '22 16:09

bh_earth0