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

前端 未结 14 1616
失恋的感觉
失恋的感觉 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: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.

提交回复
热议问题