Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL Prefixing String Literal on Insert - Any Value to This, or Redundant?

I just inherited a project that has code similar to the following (rather simple) example:

DECLARE @Demo TABLE
(
    Quantity INT,
    Symbol NVARCHAR(10)
)

INSERT INTO @Demo (Quantity, Symbol)
SELECT 127, N'IBM'

My interest is with the N before the string literal.

I understand that the prefix N is to specify encoding (in this case, Unicode). But since the select is just for inserting into a field that is clearly already Unicode, wouldn't this value be automatically upcast?

I've run the code without the N and it appears to work, but am I missing something that the previous programmer intended? Or was the N an oversight on his/her part?

I expect behavior similar to when I pass an int to a decimal field (auto-upcast). Can I get rid of those Ns?

like image 963
SethO Avatar asked May 27 '10 20:05

SethO


1 Answers

Your test is not really valid, try something like a Chinese character instead, I remember if you don't prefix it it will not insert the correct character

example, first one shows a question mark while the bottom one shows a square

select '作'

select N'作'

A better example, even here the output is not the same

declare @v nvarchar(50), @v2 nvarchar(50)

select @v = '作', @v2 = N'作'

select @v,@v2

Since what you look like is a stock table why are you using unicode, are there even symbols that are unicode..I have never seen any and this includes ISIN, CUSIPS and SEDOLS

like image 177
SQLMenace Avatar answered Oct 20 '22 12:10

SQLMenace