Should I use != or <> for not equal in T-SQL?

前端 未结 14 1618
失恋的感觉
失恋的感觉 2020-11-22 04:23

I have seen SQL that uses both != and <> for not equal. What is the preferred syntax and why?

相关标签:
14条回答
  • 2020-11-22 05:14

    It seems that Microsoft themselves prefer <> to != as evidenced in their table constraints. I personally prefer using != because I clearly read that as "not equal", but if you enter [field1 != field2] and save it as a constrait, the next time you query it, it will show up as [field1 <> field2]. This says to me that the correct way to do it is <>.

    0 讨论(0)
  • 2020-11-22 05:15

    Although they function the same way, != means exactly "not equal to", while <> means greater than and less than the value stored.

    Consider >= or <=, and this will make sense when factoring in your indexes to queries... <> will run faster in some cases (with the right index), but in some other cases (index free) they will run just the same.

    This also depends on how your databases system reads the values != and <>. The database provider may just shortcut it and make them function the same, so there isn't any benefit either way.PostgreSQL and SQL Server do not shortcut this; it is read as it appears above.

    0 讨论(0)
提交回复
热议问题