Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'N' in the where clause of SQL

Tags:

sql

sql-server

I saw this query somewhere -

SELECT *
FROM HR.Employees
WHERE country <> N'JAP';

What does that "N" mean ? I am not sure if this is valid only for SQL server.

like image 768
Steam Avatar asked Aug 29 '13 22:08

Steam


People also ask

What does N in SQL mean?

The "N" prefix stands for National Language in the SQL-92 standard, and is used for representing Unicode characters. In the current standard, it must be an upper case , which is what you will typically find implemented in mainstream products.

What is the N in NVARCHAR N )?

n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What is N value in SQL Server?

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.


1 Answers

The N stands for "National Character" and it means that the content of the string is Unicode.

You should be using Unicode (nchar/nvarchar) whenever you might come across proper names or other entities that can contain characters outside of the default ASCII character set. If you don't surround such strings with the N prefix, you will lose data. For example:

SELECT N'ук ферт хер', 'ук ферт хер';

Results:

-----------        -----------
ук ферт хер        ?? ???? ???

You should also be sure to use the N prefix in your WHERE or other clauses against n(var)char columns. When you don't use the N prefix, you could suffer serious performance issues due to implicit conversion.

like image 90
Aaron Bertrand Avatar answered Oct 07 '22 17:10

Aaron Bertrand