1

Duplicate of: Should I use != or <> for not equal in TSQL

Within SQL Server's T-SQL should I use "<>" or "!=", or does it really even matter?

Community
  • 1
  • 1
KM.
  • 98,537
  • 33
  • 172
  • 205
  • Duplicate - http://stackoverflow.com/questions/723195/should-i-use-or-for-not-equal-in-tsql/723203#723203 – madcolor Apr 09 '09 at 13:42

8 Answers8

15

I don't know SQLServer, but the SQL standard uses '<>', so follow the standard

chburd
  • 4,091
  • 27
  • 32
  • 1
    Woah woah woah, which is the T-SQL standard? I always thought != was the T-SQL, and <> being the MS-way? – Kezzer Apr 09 '09 at 13:41
  • 1
    T-SQL is short for Transact-SQL, which is Microsoft's SQL Server implementation of SQL. T-SQL supports ANSI SQL and more. – Adam Robinson Apr 09 '09 at 13:51
6

I believe that != is T-SQL specific (i.e. not standard SQL). So, you should use <> if there's any chance that you'll ever port your code to use a different DBMS.

Personally, I would use <> and forget about it.

Ferruccio
  • 96,346
  • 38
  • 221
  • 297
3

Typical SQL usage in my experience is to use <>. I've never seen anyone use !=; I wasn't even aware that worked.

Adam Robinson
  • 177,794
  • 32
  • 275
  • 339
3

!= isn't part of the standard, but it is part of T-SQL..

SQL-92 standard , T-SQL

Duplicate Post..

Should I use != or <> for not equal in TSQL?

Community
  • 1
  • 1
madcolor
  • 8,007
  • 11
  • 48
  • 74
2

Use <> since most people are familiar with that, and whenever NULLs are possible, remember that NULL is not equal to NULL, so this is sometimes necessary:

ISNULL(MY_FIELD,0) <> ISNULL(MY_FIELD,0)
JosephStyons
  • 55,647
  • 63
  • 154
  • 232
  • 2
    That would also equate a legit 0 to NULL, which may not be what you want. I always use (MY_FIELD1 <> MY_FIELD2 OR (MY_FIELD1 IS NULL AND MY_FIELD2 IS NULL)) but I'd be interested in seeing if someone else has a better way. – Misko Apr 09 '09 at 14:11
  • Yes, that's a great point. The method you describe is more reliable. – JosephStyons Apr 09 '09 at 14:28
1

If you only use SQL Server, it doesn't matter. They are synonyms.

Ed Harper
  • 20,737
  • 4
  • 54
  • 80
1

they are identical.

Look MSDN.

pocheptsov
  • 1,914
  • 23
  • 23
1

I'd say it comes down to your coding conventions. I personally like to use != as <> reminds me of dirty dirty VB and gives me a bad gut feeling. Plus I comprehend it better as it is exact to C#'s not equal operator.

Pat
  • 5,290
  • 1
  • 37
  • 53