Should I use != or <> for not equal in T-SQL?
I have seen
SQL that uses both
<> for not equal. What is the preferred syntax and why?
<> reminds me of
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...
Most databases support
!= (popular programming languages) and
Databases that support both
- MySQL 5.1:
- PostgreSQL 8.3:
- Oracle 10g:
- Microsoft SQL Server 2000/2005/2008/2012/2016:
- IBM Informix Dynamic Server 10:
- Apache Derby 10.6:
- Sybase Adaptive Server Enterprise 11.0:
Databases that support the ANSI standard operator, exclusively:
Read more… Read less…
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.
The ANSI SQL Standard defines
<> as the "not equal to" operator,
5.2 <token> and <separator>)
There is no
!= operator according to the ANSI/SQL 92 standard.
<> is the valid SQL according to the SQL-92 standard.
They're both valid and the same with respect to SQL Server,
It seems that Microsoft themselves prefer
!= 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