What is the meaning of the prefix N in T-SQL statements and when should I use it?

前端 未结 4 737
长情又很酷
长情又很酷 2020-11-22 01:01

I have seen prefix N in some insert T-SQL queries. Many people have used N before inserting the value in a table.

I searched, but I was not able to unde

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:24

    Let me tell you an annoying thing that happened with the N' prefix - I wasn't able to fix it for two days.

    My database collation is SQL_Latin1_General_CP1_CI_AS.

    It has a table with a column called MyCol1. It is an Nvarchar

    This query fails to match Exact Value That Exists.

    SELECT TOP 1 * FROM myTable1 WHERE  MyCol1 = 'ESKİ'  
    
    // 0 result
    

    using prefix N'' fixes it

    SELECT TOP 1 * FROM myTable1 WHERE  MyCol1 = N'ESKİ'  
    
    // 1 result - found!!!!
    

    Why? Because latin1_general doesn't have big dotted İ that's why it fails I suppose.

提交回复
热议问题