1

I found something weird in this query

SELECT *
FROM t1
WHERE t1.country = N'USA';

why N Letter is used ?

Nawaf
  • 540
  • 1
  • 5
  • 14

3 Answers3

3

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.

1

N is for the compatibility of a character. Used for representing unicode characters.

Newbie
  • 25
  • 7
  • You may want to look a bit longer at your answer and give something a little more succinct. – Paul Oct 10 '17 at 07:54
  • 1
    oh. sorry, my bad.. that was just my general idea. I'll do better next time captain! – Newbie Oct 10 '17 at 07:59
1

It declares the string as nvarchar data type, instead of varchar, and the difference is that varchar column only stores an 8-bit codepage and a nvarchar column can store any Unicode data

nacho
  • 5,280
  • 2
  • 25
  • 34