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

前端 未结 14 1617
失恋的感觉
失恋的感觉 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 04:48

    '<>' is from the SQL-92 standard and '!=' is a proprietary T-SQL operator. It's available in other databases as well, but since it isn't standard you have to take it on a case-by-case basis.

    In most cases, you'll know what database you're connecting to so this isn't really an issue. At worst you might have to do a search and replace in your SQL.

    0 讨论(0)
  • 2020-11-22 04:53

    I understand that the C syntax != is in SQL Server due to its Unix heritage (back in the Sybase SQL Server days, pre Microsoft SQL Server 6.5).

    0 讨论(0)
  • 2020-11-22 04:54

    I preferred using != instead of <> because sometimes I use the <s></s> syntax to write SQL commands. Using != is more handy to avoid syntax errors in this case.

    0 讨论(0)
  • 2020-11-22 04:55

    Technically they function the same if you’re using SQL Server AKA T-SQL. If you're using it in stored procedures there is no performance reason to use one over the other. It then comes down to personal preference. I prefer to use <> as it is ANSI compliant.

    You can find links to the various ANSI standards at...

    http://en.wikipedia.org/wiki/SQL

    0 讨论(0)
  • 2020-11-22 04:55

    They're both valid and the same with respect to SQL Server,

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/not-equal-to-transact-sql-exclamation

    0 讨论(0)
  • 2020-11-22 04:55

    One alternative would be to use the NULLIF operator other than <> or != which returns NULL if the two arguments are equal NULLIF in Microsoft Docs. So I believe WHERE clause can be modified for <> and != as follows:

    NULLIF(arg1, arg2) IS NOT NULL
    

    As I found that, using <> and != doesn't work for date in some cases. Hence using the above expression does the needful.

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