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. Other Discussions
  3. Clever Code
  4. Single Update Query for.....

Single Update Query for.....

Scheduled Pinned Locked Moved Clever Code
databasequestionannouncement
17 Posts 5 Posters 25 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.
  • S Sanjay K Gupta

    You have updated the Id. What would you do if there may some more columns like Address and Mobile.... Only Update Name column. The result should be.... Id Address Name --------------------------------------- 1 Address1 B 2 Address2 A 3 Address3 D 4 Address4 C . .... . . .... .

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #4

    you could do similar exor operations on the ASCII ordinal of a 1-character "name" field. :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    S 1 Reply Last reply
    0
    • L Luc Pattyn

      you could do similar exor operations on the ASCII ordinal of a 1-character "name" field. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      S Offline
      S Offline
      Sanjay K Gupta
      wrote on last edited by
      #5

      Thank you Sir! Today I have learned new things about Bitwise XOR. For updating name column, the query may be like .. UPDATE trns.a SET Name=CHAR(1+((ASCII(Name)-1)^1)). But if the Name column is not a single character eg ('Alexzendar','Bob','Caldle','Delle','Ellen','Fusion')??

      L 1 Reply Last reply
      0
      • S Sanjay K Gupta

        Thank you Sir! Today I have learned new things about Bitwise XOR. For updating name column, the query may be like .. UPDATE trns.a SET Name=CHAR(1+((ASCII(Name)-1)^1)). But if the Name column is not a single character eg ('Alexzendar','Bob','Caldle','Delle','Ellen','Fusion')??

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #6

        Then you would need a different approach, and a better example. BTW: this forum is for showing existing clever code, not for asking questions that may need clever code to get resolved. As such it is the antipode of the "Hall of Shame". :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        1 Reply Last reply
        0
        • S Sanjay K Gupta

          create table trns.a(id int,name varchar(20))
          insert into TRNS.a values(1,'A')
          insert into TRNS.a values(2,'B')
          insert into TRNS.a values(3,'C')
          insert into TRNS.a values(4,'D')
          insert into TRNS.a values(5,'E')
          insert into TRNS.a values(6,'F')

          select *from a
          if you run select Query, you will get
          id name

          1 A
          2 B
          3 C
          4 D
          . .
          . .

          Q. I need to Update the above table in single update statement to get result like
          select *from a

          id name

          1 B
          2 A
          3 D
          4 C
          . .
          . .

          Note: I know the solution, So I have put this question under "Clever Code".

          A Offline
          A Offline
          Al_Brown
          wrote on last edited by
          #7

          Slight modification of Luc's idea:

          UPDATE a INNER JOIN a AS a2 ON a.id = 1+((a2.id-1)^1) SET a.name = a2.name

          S 1 Reply Last reply
          0
          • A Al_Brown

            Slight modification of Luc's idea:

            UPDATE a INNER JOIN a AS a2 ON a.id = 1+((a2.id-1)^1) SET a.name = a2.name

            S Offline
            S Offline
            Sanjay K Gupta
            wrote on last edited by
            #8

            No, your query is giving error.. Incorrect syntax near the keyword 'INNER'.

            A 1 Reply Last reply
            0
            • S Sanjay K Gupta

              No, your query is giving error.. Incorrect syntax near the keyword 'INNER'.

              A Offline
              A Offline
              Al_Brown
              wrote on last edited by
              #9

              Which database system are you using? Try it without the word INNER.

              S 1 Reply Last reply
              0
              • A Al_Brown

                Which database system are you using? Try it without the word INNER.

                S Offline
                S Offline
                Sanjay K Gupta
                wrote on last edited by
                #10

                SQL Server 2008

                A 1 Reply Last reply
                0
                • S Sanjay K Gupta

                  SQL Server 2008

                  A Offline
                  A Offline
                  Al_Brown
                  wrote on last edited by
                  #11

                  Ah, OK. In that case use:

                  UPDATE a SET a.name = a2.name FROM a INNER JOIN a AS a2 ON a.id = 1+((a2.id-1)^1)

                  S 1 Reply Last reply
                  0
                  • A Al_Brown

                    Ah, OK. In that case use:

                    UPDATE a SET a.name = a2.name FROM a INNER JOIN a AS a2 ON a.id = 1+((a2.id-1)^1)

                    S Offline
                    S Offline
                    Sanjay K Gupta
                    wrote on last edited by
                    #12

                    Clever Code!! That is what I was for looking for. Thanks. .. But here is id in sequence. Consider the condition when Id column is not present then how could you update Odd row to Next Even Row and Even Row to Previous Odd Row.

                    A 1 Reply Last reply
                    0
                    • S Sanjay K Gupta

                      Clever Code!! That is what I was for looking for. Thanks. .. But here is id in sequence. Consider the condition when Id column is not present then how could you update Odd row to Next Even Row and Even Row to Previous Odd Row.

                      A Offline
                      A Offline
                      Al_Brown
                      wrote on last edited by
                      #13

                      This is just theoretical, right....? You could do something like this:

                      UPDATE a SET name = (SELECT TOP 1 a2.name FROM a AS a2
                      WHERE a2.id * ((a.id & 1) * 2 - 1) > a.id * ((a.id & 1) * 2 - 1)
                      AND (a2.id ^ a.id) & 1 = 1
                      ORDER BY a2.id * ((a.id & 1) * 2 - 1))

                      But then you're going into Hall Of Shame territory

                      1 Reply Last reply
                      0
                      • S Sanjay K Gupta

                        create table trns.a(id int,name varchar(20))
                        insert into TRNS.a values(1,'A')
                        insert into TRNS.a values(2,'B')
                        insert into TRNS.a values(3,'C')
                        insert into TRNS.a values(4,'D')
                        insert into TRNS.a values(5,'E')
                        insert into TRNS.a values(6,'F')

                        select *from a
                        if you run select Query, you will get
                        id name

                        1 A
                        2 B
                        3 C
                        4 D
                        . .
                        . .

                        Q. I need to Update the above table in single update statement to get result like
                        select *from a

                        id name

                        1 B
                        2 A
                        3 D
                        4 C
                        . .
                        . .

                        Note: I know the solution, So I have put this question under "Clever Code".

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #14

                        You could have made this a Friday Programming Quiz. UPDATE a SET id = CASE WHEN id/2.0=CAST(id/2.0 AS INTEGER) THEN id-1 ELSE id+1 END

                        1 Reply Last reply
                        0
                        • S Sanjay K Gupta

                          You have updated the Id. What would you do if there may some more columns like Address and Mobile.... Only Update Name column. The result should be.... Id Address Name --------------------------------------- 1 Address1 B 2 Address2 A 3 Address3 D 4 Address4 C . .... . . .... .

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #15

                          That was not part of the specification.

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            UPDATE trns.a SET id=1+((id-1)^1)

                            :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            I Offline
                            I Offline
                            ian dennis 0
                            wrote on last edited by
                            #16

                            Surely anything to the power of 1 is itself? so (id-1)^1 = id-1

                            L 1 Reply Last reply
                            0
                            • I ian dennis 0

                              Surely anything to the power of 1 is itself? so (id-1)^1 = id-1

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #17

                              ian dennis wrote:

                              anything to the power of 1 is itself

                              yes

                              ian dennis wrote:

                              so (id-1)^1 = id-1

                              no, that is simply never true in the programming languages I use. Including SQL. :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum


                              Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.

                              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