N' annotation

  • 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?

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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)

     

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • As others have said, the presence/absence of the N determines the data of the literal. Try this:

    SELECT '??fa', N'??fa'

    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.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • 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

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply