Results 1 to 11 of 11

Thread: [RESOLVED] <> or !=

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Resolved [RESOLVED] <> or !=

    Quick question, folks: In SQL, is there any reason to prefer <> or != over the other, or is it simply personal preference? Is one standard and one non-standard?

    Annoyingly, they're very hard terms to Google!
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: <> or !=

    I'm pretty sure != isn't part of the ansii standard so may not be supported in all flavours of sql (I actually had to go and try it out in SQL Server to see if it works... it does).

    It's also worth mentioning that <> does not mean "Not Equal To", it means "Is Greater Than Or Less Than". It's pedantic but those two meanings are subtly different and can kick you in the pants in some rare edge cases. I've therefore always preferred to use the NOT keyword along with an equals, ie "Where Not 1=0"
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: <> or !=

    It's pedantic but those two meanings are subtly different and can kick you in the pants in some rare edge cases
    I'd like to hear more about such case because in over 20 years I've never run into a problem with <> not operating as expected (as a not equals) ...

    I didn't know that != worked in SQL Server, good to know.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: <> or !=

    Just talking about MS SQL server (no idea about any others), != actually gets translated to <> on the backend, so they're the same.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: <> or !=

    Well, that's what I thought too... but the comment from Funky seemed to indicate there was a subtle difference that he had encountered at some point. Since I've never encountered such a situation, I was curious to know more about it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: <> or !=

    Quote Originally Posted by kfcSmitty View Post
    Just talking about MS SQL server (no idea about any others), != actually gets translated to <> on the backend, so they're the same.
    I didn't know this.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: <> or !=

    I guess that, as a .NET developer using SQL Server or some other database that supports both, it might depend on whether you come from a C# or VB background. That's basically personal preference though so doesn't provide the possible technical reason that you were looking for.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: <> or !=

    Quote Originally Posted by FunkyDexter View Post
    It's also worth mentioning that <> does not mean "Not Equal To", it means "Is Greater Than Or Less Than".
    Is that really true though? That's what the operator looks like but the documentation for both VB and T-SQL define it as meaning "not equal to". Maybe there is some other language where that subtle distinction does exist. I guess one scenario where it might make a difference is when dealing with NULLs. NULL is not equal to any other value, even NULL, but it is not less than or greater than. If that operator was interpreted as you say then you'd have to be aware of that when dealing with NULLs.

    The T-SQL documentation for the != operator also says:
    Functions the same as the <> (Not Equal To) comparison operator.
    so that addresses the original question, at least in terms of T-SQL.

    Microsoft have languages that use both so probably thought that supporting both in T-SQL was a good idea.

  9. #9

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: <> or !=

    Thanks, guys. I hadn't seen != in years, but came across a whole clutch of them in a set of stored procedures written by a 3rd party. I was just wondering if they knew something I didn't.

    I think I'll stick with <>, although I'm now curious about Funky's comments that it doesn't always perform as expected.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  10. #10

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: <> or !=

    Oops - double post!
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  11. #11
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: <> or !=

    Wow, I wasn't expecting my comments on <> to cause such controversy.

    I was talking from a purely mathematical point of view rather than a sql (ansii or flavoured) point of view. To my knowledge <> is equivalent to "Not Equal To" in TSql at least. I prefer to use NOT and = (or ==) because it's mathematically precise so avoids any language based nuances - it always has the same meaning no matter where I am.

    I do have a vague memory of getting kicked in the teeth by <> once but I can't remember the context very well. I remember it was when writing client code rather than sql and I seem to remember it was to do with checking non-equivalence rather than non-equality and I think it was something to do with string comparisons. I'm pretty sure it wasn't to do with nulls though because, of course, you have to use IS to do a compare on them - in sql at least. Not =, <> or != would all give the wrong result.

    Anyway, the upshot is, if you're using <> in TSQL, to my knowledge you're safe.
    Last edited by FunkyDexter; Jul 24th, 2014 at 04:44 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width