Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL boolean comparison

SQL boolean comparison

Scheduled Pinned Locked Moved Database
databasehelpquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Interesting, why this...

    SELECT
    COUNT(*)
    FROM TBL1
    LEFT JOIN TBL2 ON TBL2.ID = TBL1.TBL2_ID
    WHERE (TBL1.INTFIELD IS NULL) <> (TBL2.INTFIELD IS NULL)

    ... Ends in an error:

    Msg 102, Level 15, State 1, Line 5
    Incorrect syntax near '<'.

    Where I'm wrong? Is it not allowed to compare two booleans? At least in (MS)SQL not :doh:

    M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #3

    Try using a case statement in the where clause to turn the bool in 1 or 0 where (case when intfield is null then 0 else 1 end) != (case when intfield is null then 0 else 1 end) (why do you have int field that are null?)

    Never underestimate the power of human stupidity RAH

    L 1 Reply Last reply
    0
    • L Lost User

      I think boolean expressions require boolean operators: AND, OR etc.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Yes seems so in SQL. But e.g. in c++ (and I think also in c#) you can easy have something like:

      bool b1= true;
      bool b2= false;
      bool res= b1 == b2;

      L 1 Reply Last reply
      0
      • M Mycroft Holmes

        Try using a case statement in the where clause to turn the bool in 1 or 0 where (case when intfield is null then 0 else 1 end) != (case when intfield is null then 0 else 1 end) (why do you have int field that are null?)

        Never underestimate the power of human stupidity RAH

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Why int field available NULL: To know wheter there have assigned a value. BTW the same "Problem" will be for any other datatype I assume. case: Would be nice to have it. At the Moment I have to fight with Interbase SQL :(

        1 Reply Last reply
        0
        • L Lost User

          Yes seems so in SQL. But e.g. in c++ (and I think also in c#) you can easy have something like:

          bool b1= true;
          bool b2= false;
          bool res= b1 == b2;

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          (SQL != C++) && (SQL != C#)

          L 2 Replies Last reply
          0
          • L Lost User

            (SQL != C++) && (SQL != C#)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            No ...

            (SQL != C++) && (SQL != C#)

            ... does also not work :laugh:

            1 Reply Last reply
            0
            • L Lost User

              Interesting, why this...

              SELECT
              COUNT(*)
              FROM TBL1
              LEFT JOIN TBL2 ON TBL2.ID = TBL1.TBL2_ID
              WHERE (TBL1.INTFIELD IS NULL) <> (TBL2.INTFIELD IS NULL)

              ... Ends in an error:

              Msg 102, Level 15, State 1, Line 5
              Incorrect syntax near '<'.

              Where I'm wrong? Is it not allowed to compare two booleans? At least in (MS)SQL not :doh:

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #8

              You need a logical operator to combine two boolean conditions. In this example, you'd want the Xor operator. But unfortunately, MS SQL Server doesn't support it; it only provides a bitwise Xor operator, which won't work here. You can either use CASE, as Mycroft suggested, or the longhand version of Xor:

              a XOR b <==> (a AND (NOT b)) OR ((NOT a) AND b)

              ... WHERE ((TBL1.INTFIELD Is Null And TBL2.INTFIELD Is Not Null) Or (TBL1.INTFIELD Is Not Null And TBL2.INTFIELD Is Null))


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              L 2 Replies Last reply
              0
              • Richard DeemingR Richard Deeming

                You need a logical operator to combine two boolean conditions. In this example, you'd want the Xor operator. But unfortunately, MS SQL Server doesn't support it; it only provides a bitwise Xor operator, which won't work here. You can either use CASE, as Mycroft suggested, or the longhand version of Xor:

                a XOR b <==> (a AND (NOT b)) OR ((NOT a) AND b)

                ... WHERE ((TBL1.INTFIELD Is Null And TBL2.INTFIELD Is Not Null) Or (TBL1.INTFIELD Is Not Null And TBL2.INTFIELD Is Null))


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Thank you for your answer. "The longhead version of Xor": That is the Thing a tried to write shorter when I one time again was struggling with the same described here: The Weird and The Wonderful[^] :)

                1 Reply Last reply
                0
                • L Lost User

                  (SQL != C++) && (SQL != C#)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  And SQL := SQL. Only for Information, MySQL 5.6 does evaluate it like I would expect.

                  L 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    You need a logical operator to combine two boolean conditions. In this example, you'd want the Xor operator. But unfortunately, MS SQL Server doesn't support it; it only provides a bitwise Xor operator, which won't work here. You can either use CASE, as Mycroft suggested, or the longhand version of Xor:

                    a XOR b <==> (a AND (NOT b)) OR ((NOT a) AND b)

                    ... WHERE ((TBL1.INTFIELD Is Null And TBL2.INTFIELD Is Not Null) Or (TBL1.INTFIELD Is Not Null And TBL2.INTFIELD Is Null))


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Only for Information: MySQL 5.6 does evaluate it, like I expect it has to be evaluated. Tested on SQL Fidle.

                    1 Reply Last reply
                    0
                    • L Lost User

                      And SQL := SQL. Only for Information, MySQL 5.6 does evaluate it like I would expect.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      I think you mean SQL != MySQL. Different SQL implementations have different features, that is well known.

                      1 Reply Last reply
                      0
                      • L Lost User

                        I think boolean expressions require boolean operators: AND, OR etc.

                        U Offline
                        U Offline
                        User 13364865
                        wrote on last edited by
                        #13

                        NULLs cannot be compared in SQL

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups