Asdfg
Asdfg

Reputation: 12253

Database - what is the different between 3 and N'3' in TSQL (Sql Server 2008)

Assuming ProductId is INT column, what is the difference between writing 3 and N'3'?

They both work fine.

INSERT INTO dbo.Products (ProductId, Name) VALUES (3,'Product3')

INSERT INTO dbo.Products (ProductId, Name) VALUES (N'3','Product3')

Upvotes: 0

Views: 306

Answers (2)

Derek
Derek

Reputation: 23308

When SQL Server encounters N'', it will treat the string as an NVARCHAR instead of VARCHAR. NVARCHAR fields are unicode.

The reason N'3' still works for you is because it is converting it to NVARCHAR, and then doing an implicit conversion to INT.

Upvotes: 2

Yuck
Yuck

Reputation: 50855

N'3' is a nvarchar type, where 3 is an int. If you're using nvarchar you're asking the parser to perform implicit conversion. In this case it works fine but in others you may run into casting problems.

Upvotes: 9

Related Questions