SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


N' annotation


N' annotation

Author
Message
grecci1077
grecci1077
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 91
I am still trying to make sense of the whole n' when selecting text or varchar values... Is that so necessary? Can someone give me an example?
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17825 Visits: 6431
For display purposes these 2 results would be identical (except of course for the specific words):


SELECT 'THIS IS A VARCHAR STRING', N'THIS IS AN NVARCHAR STRING';




There may be a slight performance boost in a case where you do an assignment to a type VARCHAR variable or column, like:


DECLARE @MyString NVARCHAR(100);

SELECT @MyString = N'THIS IS AN NVARCHAR STRING';




Because if you omit the N' annotation, SQL would need to do an implicit conversion to the NVARCHAR type. So the annotation simply defines the string that follows as Unicode string.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Steven Willis
Steven Willis
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 1721
If you ever use dynamic SQL using the built-in system function 'sp_executeSQL' and try to use an OUTPUT parameter the function will not work unless you explicitly append the N' prefix.

sp_executesql (Transact-SQL)


 
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)SSC Guru (215K reputation)

Group: General Forum Members
Points: 215164 Visits: 41979
grecci1077 (7/31/2013)
I am still trying to make sense of the whole n' when selecting text or varchar values... Is that so necessary? Can someone give me an example?


To summarize what the other folks on this thread have said, the N' is how you mark string literals as NVARCHAR literals. If you're working with NVARCHAR datatypes, such as when they appear in sp_ExecuteSQL or table columns with an NVARCHAR datatype, it's best to use the N' prefix. It's also best NOT to use it on string literals when comparing to a VARCHAR column in a table because using the N' prefix on a literal to be compared to a VARCHAR column requires that the whole column of data must be converted before the comparison can be made. That's obviously a "Non SARGable" comparison of the worst kind.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17825 Visits: 6431
Steven Willis (7/31/2013)
If you ever use dynamic SQL using the built-in system function 'sp_executeSQL' and try to use an OUTPUT parameter the function will not work unless you explicitly append the N' prefix.

sp_executesql (Transact-SQL)


 


Steven - That is a very good point. I've seen several cases where sp_executesql is sensitive to passing in only N' strings.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Erland Sommarskog
Erland Sommarskog
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5224 Visits: 875
As others have said, the presence/absence of the N determines the data of the literal. Try this:

SELECT 'άλφα', N'άλφα'



As Jeff pointed out, you should not use nvarchar just to be safe, but pick the data type that fits your context. Else you can cause performance disasters.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
siggemannen
siggemannen
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 Visits: 380
I've noticed when you have code like:

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = '... very long sql' + CAST(@someparam AS VARCHAR) + 'continuation of long sql' + '...'




And your @SQL is nearing 4k character limit, you need to explicitely convert all the strings into NVARCHAR(MAX) and use + N'continuation of sql...' strings
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search