Update all date columns in SQL Server -1 day










1















I want to update my database (SQL Server Express) all the dates for specific ids.



I am displaying the ids I want to.



SELECT TOP (1000) ID, Dates
FROM tbl_table
WHERE (id IN (29695, 29700, 29701, 29702, 29703, 29704, 29705, 29706, 29707, 29708, 29709, 29710, 29711, 29712, 29713, 29714, 29715))


AND my dates in the database are like this:



enter image description here



Is there any way to update all the date columns with same date - 1 day?



For example: if we have 2019-12-20, update it to 2019-12-19?



For example if I want to do it in PHP, I would loop through this query and fetch all all dates. After I would remove one day like this:



date('m/d/Y', strtotime($date. ' - 1 days');


And create a query to update all the columns based on id. I just want to avoid that. Is there any SQL command that do that?



Thanks










share|improve this question




























    1















    I want to update my database (SQL Server Express) all the dates for specific ids.



    I am displaying the ids I want to.



    SELECT TOP (1000) ID, Dates
    FROM tbl_table
    WHERE (id IN (29695, 29700, 29701, 29702, 29703, 29704, 29705, 29706, 29707, 29708, 29709, 29710, 29711, 29712, 29713, 29714, 29715))


    AND my dates in the database are like this:



    enter image description here



    Is there any way to update all the date columns with same date - 1 day?



    For example: if we have 2019-12-20, update it to 2019-12-19?



    For example if I want to do it in PHP, I would loop through this query and fetch all all dates. After I would remove one day like this:



    date('m/d/Y', strtotime($date. ' - 1 days');


    And create a query to update all the columns based on id. I just want to avoid that. Is there any SQL command that do that?



    Thanks










    share|improve this question


























      1












      1








      1








      I want to update my database (SQL Server Express) all the dates for specific ids.



      I am displaying the ids I want to.



      SELECT TOP (1000) ID, Dates
      FROM tbl_table
      WHERE (id IN (29695, 29700, 29701, 29702, 29703, 29704, 29705, 29706, 29707, 29708, 29709, 29710, 29711, 29712, 29713, 29714, 29715))


      AND my dates in the database are like this:



      enter image description here



      Is there any way to update all the date columns with same date - 1 day?



      For example: if we have 2019-12-20, update it to 2019-12-19?



      For example if I want to do it in PHP, I would loop through this query and fetch all all dates. After I would remove one day like this:



      date('m/d/Y', strtotime($date. ' - 1 days');


      And create a query to update all the columns based on id. I just want to avoid that. Is there any SQL command that do that?



      Thanks










      share|improve this question
















      I want to update my database (SQL Server Express) all the dates for specific ids.



      I am displaying the ids I want to.



      SELECT TOP (1000) ID, Dates
      FROM tbl_table
      WHERE (id IN (29695, 29700, 29701, 29702, 29703, 29704, 29705, 29706, 29707, 29708, 29709, 29710, 29711, 29712, 29713, 29714, 29715))


      AND my dates in the database are like this:



      enter image description here



      Is there any way to update all the date columns with same date - 1 day?



      For example: if we have 2019-12-20, update it to 2019-12-19?



      For example if I want to do it in PHP, I would loop through this query and fetch all all dates. After I would remove one day like this:



      date('m/d/Y', strtotime($date. ' - 1 days');


      And create a query to update all the columns based on id. I just want to avoid that. Is there any SQL command that do that?



      Thanks







      sql sql-server database date datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 21 '18 at 17:51









      marc_s

      575k12811111258




      575k12811111258










      asked Mar 21 '18 at 16:16









      MariaMaria

      379314




      379314






















          3 Answers
          3






          active

          oldest

          votes


















          3














          The request below will update the rows you want by adding -1 days on each date:



          UPDATE tbl_table 
          SET dates = Dateadd(day, -1, dates)
          WHERE id IN ( 29695, 29700, 29701, 29702,
          29703, 29704, 29705, 29706,
          29707, 29708, 29709, 29710,
          29711, 29712, 29713, 29714, 29715 )


          DATEADD function takes 3 parameters:



          1. the interval ( day, month, year ..)

          2. the increment (the value to add or remove if negative)

          3. an expression (wich is a datetime type)

          See DATEADD documentation






          share|improve this answer
































            2














            To return a query with the previous day:



            SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
            FROM tbl_table


            To update the table with the previous day:



            UPDATE tbl_table
            SET Dates = DATEADD(dd, -1, Dates)
            FROM -- Put your conditions here





            share|improve this answer






























              0














              UPDATE tableName SET date= DATEADD(d,-1, date) 
              where ....


              ( here you put where clause for you required)






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49411498%2fupdate-all-date-columns-in-sql-server-1-day%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                The request below will update the rows you want by adding -1 days on each date:



                UPDATE tbl_table 
                SET dates = Dateadd(day, -1, dates)
                WHERE id IN ( 29695, 29700, 29701, 29702,
                29703, 29704, 29705, 29706,
                29707, 29708, 29709, 29710,
                29711, 29712, 29713, 29714, 29715 )


                DATEADD function takes 3 parameters:



                1. the interval ( day, month, year ..)

                2. the increment (the value to add or remove if negative)

                3. an expression (wich is a datetime type)

                See DATEADD documentation






                share|improve this answer





























                  3














                  The request below will update the rows you want by adding -1 days on each date:



                  UPDATE tbl_table 
                  SET dates = Dateadd(day, -1, dates)
                  WHERE id IN ( 29695, 29700, 29701, 29702,
                  29703, 29704, 29705, 29706,
                  29707, 29708, 29709, 29710,
                  29711, 29712, 29713, 29714, 29715 )


                  DATEADD function takes 3 parameters:



                  1. the interval ( day, month, year ..)

                  2. the increment (the value to add or remove if negative)

                  3. an expression (wich is a datetime type)

                  See DATEADD documentation






                  share|improve this answer



























                    3












                    3








                    3







                    The request below will update the rows you want by adding -1 days on each date:



                    UPDATE tbl_table 
                    SET dates = Dateadd(day, -1, dates)
                    WHERE id IN ( 29695, 29700, 29701, 29702,
                    29703, 29704, 29705, 29706,
                    29707, 29708, 29709, 29710,
                    29711, 29712, 29713, 29714, 29715 )


                    DATEADD function takes 3 parameters:



                    1. the interval ( day, month, year ..)

                    2. the increment (the value to add or remove if negative)

                    3. an expression (wich is a datetime type)

                    See DATEADD documentation






                    share|improve this answer















                    The request below will update the rows you want by adding -1 days on each date:



                    UPDATE tbl_table 
                    SET dates = Dateadd(day, -1, dates)
                    WHERE id IN ( 29695, 29700, 29701, 29702,
                    29703, 29704, 29705, 29706,
                    29707, 29708, 29709, 29710,
                    29711, 29712, 29713, 29714, 29715 )


                    DATEADD function takes 3 parameters:



                    1. the interval ( day, month, year ..)

                    2. the increment (the value to add or remove if negative)

                    3. an expression (wich is a datetime type)

                    See DATEADD documentation







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 8:47









                    octano

                    208415




                    208415










                    answered Mar 21 '18 at 16:19









                    KobiKobi

                    2,163924




                    2,163924























                        2














                        To return a query with the previous day:



                        SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
                        FROM tbl_table


                        To update the table with the previous day:



                        UPDATE tbl_table
                        SET Dates = DATEADD(dd, -1, Dates)
                        FROM -- Put your conditions here





                        share|improve this answer



























                          2














                          To return a query with the previous day:



                          SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
                          FROM tbl_table


                          To update the table with the previous day:



                          UPDATE tbl_table
                          SET Dates = DATEADD(dd, -1, Dates)
                          FROM -- Put your conditions here





                          share|improve this answer

























                            2












                            2








                            2







                            To return a query with the previous day:



                            SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
                            FROM tbl_table


                            To update the table with the previous day:



                            UPDATE tbl_table
                            SET Dates = DATEADD(dd, -1, Dates)
                            FROM -- Put your conditions here





                            share|improve this answer













                            To return a query with the previous day:



                            SELECT TOP (1000) ID, Dates, DATEADD(dd, -1, Dates) AS PreviousDay
                            FROM tbl_table


                            To update the table with the previous day:



                            UPDATE tbl_table
                            SET Dates = DATEADD(dd, -1, Dates)
                            FROM -- Put your conditions here






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 21 '18 at 16:22









                            RyanRyan

                            6,31822232




                            6,31822232





















                                0














                                UPDATE tableName SET date= DATEADD(d,-1, date) 
                                where ....


                                ( here you put where clause for you required)






                                share|improve this answer





























                                  0














                                  UPDATE tableName SET date= DATEADD(d,-1, date) 
                                  where ....


                                  ( here you put where clause for you required)






                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    UPDATE tableName SET date= DATEADD(d,-1, date) 
                                    where ....


                                    ( here you put where clause for you required)






                                    share|improve this answer















                                    UPDATE tableName SET date= DATEADD(d,-1, date) 
                                    where ....


                                    ( here you put where clause for you required)







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 13 '18 at 8:21









                                    Vineeth Sai

                                    2,48751323




                                    2,48751323










                                    answered Nov 13 '18 at 7:58









                                    zulqarnainzulqarnain

                                    1




                                    1



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49411498%2fupdate-all-date-columns-in-sql-server-1-day%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Darth Vader #20

                                        How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                        Ondo