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

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

    Most databases support != (popular programming languages) and <> (ANSI).

    Databases that support both != and <>:

    • MySQL 5.1: != and <>
    • PostgreSQL 8.3: != and <>
    • SQLite: != and <>
    • Oracle 10g: != and <>
    • Microsoft SQL Server 2000/2005/2008/2012/2016: != and <>
    • IBM Informix Dynamic Server 10: != and <>
    • InterBase/Firebird: != and <>
    • Apache Derby 10.6: != and <>
    • Sybase Adaptive Server Enterprise 11.0: != and <>

    Databases that support the ANSI standard operator, exclusively:

    • IBM DB2 UDB 9.5: <>
    • Microsoft Access 2010: <>
    0 讨论(0)
  • 2020-11-22 04:58

    !=, despite being non-ANSI, is more in the true spirit of SQL as a readable language. It screams not equal. <> says it's to me (less than, greater than) which is just weird. I know the intention is that it's either less than or greater than hence not equal, but that's a really complicated way of saying something really simple.

    I've just had to take some long SQL queries and place them lovingly into an XML file for a whole bunch of stupid reasons I won't go into.

    Suffice to say XML is not down with <> at all and I had to change them to != and check myself before I riggedy wrecked myself.

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

    They are both accepted in T-SQL. However, it seems that using <> works a lot faster than !=. I just ran a complex query that was using !=, and it took about 16 seconds on average to run. I changed those to <> and the query now takes about 4 seconds on average to run. That's a huge improvement!

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

    <> is the valid SQL according to the SQL-92 standard.

    http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx

    0 讨论(0)
  • You can use whichever you like in T-SQL. The documentation says they both function the same way. I prefer !=, because it reads "not equal" to my (C/C++/C# based) mind, but database gurus seem to prefer <>.

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

    The ANSI SQL Standard defines <> as the "not equal to" operator,

    http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt (5.2 <token> and <separator>)

    There is no != operator according to the ANSI/SQL 92 standard.

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