0

I have been reading SQL best practice examples and notice that when filtering a nvarchar column using the where clause, it is best practise to specify the keyword N prior to the text being searched.

Example: where column1=N'text'

In above example I am filtering column1 (nvarchar column) on the string 'text'.

I want to understand what is the implication of not following this method (that is - not specifying the N keyword while filtering nvarchar coloumns)

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
variable
  • 8,262
  • 9
  • 95
  • 215
  • I would assume it avoids allocating another bit of memory to convert the varchar 'text' into a nvarchar. – Candide Apr 12 '17 at 15:18
  • Please check the following link if it helps: http://stackoverflow.com/questions/10025032/what-is-the-meaning-of-the-prefix-n-in-t-sql-statements – Biswabid Apr 12 '17 at 15:18
  • Thanks i am aware about the N keyword and its use. So does using this keyword result in better performance? Or different results? I just need to understand the implication interms of result/performance please – variable Apr 12 '17 at 15:24
  • One example, try this: exec sp_executesql 'select 1' vs exec sp_executesql N'select 1' The question linked by @Biswabid has some good info, particularly the quoted part about conversion to default code page when not using N. – Gareth Lyons Apr 12 '17 at 15:26
  • 1
    I'd bet there isn't a difference. When comparing two different data types (in a join clause, etc) the 'smaller' datatype gets converted to the 'larger' data type. In other words, your 'text' automatically gets converted to N'text'. The conversion only happens once, so no big deal. If it were the other way around, though, and you had a varchar column and an nvarchar constant, each entry in the varchar column would need conversion -- and no indices could be used. – user1935361 Apr 12 '17 at 15:28

1 Answers1

1

On using N if there is any foreign language character available like French æ , it will be considered as æ and not as an English language character 'ae'. This will not produce any mismatch while data comparison of an incoming text with the existing text.

Kapil
  • 987
  • 5
  • 11