Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of the N 1

Status
Not open for further replies.

abdshall

IS-IT--Management
Mar 14, 2008
3
0
0
Hello all

I'm a Junior SQL programmer and I have a question about adding the N in front of some declarations.
For example,
SET @db_id = DB_ID(N'AdventureWorks');

What is the N used for and when do I need to add it?


Thank you
 
The N identifies the string following it as UNICODE. You use the N whenever you need to hard code a unicode string.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
When do you think I would I have a problem if I don't use it?

Thank you
 
When you are actually dealing with unicode data.

Unfortunately, Tek-tips does not support unicode posts, so it's a little difficult to show.

[tt][blue]
Declare @Temp VarChar(20)

Set @Temp = N'[Ω]'

Select @Temp, N'[Ω]'
[/blue][/tt]

When you run this code, you will see the Capital Letter O returned for the @Temp variable. This happens because it is defined as varchar(20). If you change this to....

[tt][blue]
Declare @Temp [!]n[/!]VarChar(20)

Set @Temp = '[Ω]'
Select @Temp, N'[Ω]'
[/blue][/tt]

You will get the same result eventhough @Temp is now defined as nvarchar(20).

If you truly want to set @Temp to be the 'omega' symbol, you must do it this way...

[tt][blue]
Declare @Temp [!]n[/!]VarChar(20)

Set @Temp = [!]N[/!]'[Ω]'
Select @Temp, N'[Ω]'
[/blue][/tt]

When you run that last code block, you will see [Ω] displayed for the @Temp variable.

Of course, I used the greek letter Omega as an example, but you would have similar problems for other Non-Ascii data, like Japanese and Chinese characters.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
gmmastros

Thank you for the reply,it does make sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top