Round datetime to the beginning of the current hour












1















I would like to round a datetime (should also return a datetime) to the beginning of the hour it's on, so for example:



2018-09-22 11:31:42.411 -> 2018-09-22 11:00:00.000
2018-09-22 11:08:02.456 -> 2018-09-22 11:00:00.000
2018-09-22 11:52:08.005 -> 2018-09-22 11:00:00.000


Can someone help with this please?










share|improve this question





























    1















    I would like to round a datetime (should also return a datetime) to the beginning of the hour it's on, so for example:



    2018-09-22 11:31:42.411 -> 2018-09-22 11:00:00.000
    2018-09-22 11:08:02.456 -> 2018-09-22 11:00:00.000
    2018-09-22 11:52:08.005 -> 2018-09-22 11:00:00.000


    Can someone help with this please?










    share|improve this question



























      1












      1








      1








      I would like to round a datetime (should also return a datetime) to the beginning of the hour it's on, so for example:



      2018-09-22 11:31:42.411 -> 2018-09-22 11:00:00.000
      2018-09-22 11:08:02.456 -> 2018-09-22 11:00:00.000
      2018-09-22 11:52:08.005 -> 2018-09-22 11:00:00.000


      Can someone help with this please?










      share|improve this question
















      I would like to round a datetime (should also return a datetime) to the beginning of the hour it's on, so for example:



      2018-09-22 11:31:42.411 -> 2018-09-22 11:00:00.000
      2018-09-22 11:08:02.456 -> 2018-09-22 11:00:00.000
      2018-09-22 11:52:08.005 -> 2018-09-22 11:00:00.000


      Can someone help with this please?







      sql sql-server datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 12:55









      Salman A

      185k67345441




      185k67345441










      asked Nov 26 '18 at 11:10









      AzhariAzhari

      256




      256
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Assuming the datatype is DATETIME or DATETIME2 you can use this common trick:



          SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, yourdate), 0)


          DATEDIFF(HOUR, 0, yourdate) will give you the number of hours (and nothing else) between the zeroth date and yourdate. When you add those hours back to the zeroth date you get yourdate "floored" to the hour.






          share|improve this answer


























          • This does exactly what I need it to do, thank you!

            – Azhari
            Nov 26 '18 at 11:26



















          3














          Another method is the DATEADD, DATEDIFF:



          SELECT DATEADD(HOUR, DATEDIFF(HOUR, '20000101',YourColumn),'20000101') AS RoundedValue
          FROM YourTable;





          share|improve this answer


























          • Can you explain what this is doing?

            – Azhari
            Nov 26 '18 at 11:22











          • @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

            – Larnu
            Nov 26 '18 at 11:23











          • I need it to round to the beginning of the current hour

            – Azhari
            Nov 26 '18 at 11:24











          • @Azhari and that's what it does.

            – Larnu
            Nov 26 '18 at 11:24






          • 1





            Small comment, there is a typo in the first date, one 0 missing.

            – SQL_M
            Nov 26 '18 at 11:28



















          1














          You can use DATETIMEFROMPARTS() Function.



          select DATETIMEFROMPARTS(YEAR(@date),MONTH(@date),DAY(@date),DATEPART(HOUR,@date),00,00,00)


          Here @date is replaced with YourColumn or any variable.






          share|improve this answer































            0














            This converts the value to a string, takes the left 14 chars and add the 00:00:00 values



            select  left(convert(varchar , datecol, 121),14) + '00:00:00'  as mydatecol from table





            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%2f53479857%2fround-datetime-to-the-beginning-of-the-current-hour%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Assuming the datatype is DATETIME or DATETIME2 you can use this common trick:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, yourdate), 0)


              DATEDIFF(HOUR, 0, yourdate) will give you the number of hours (and nothing else) between the zeroth date and yourdate. When you add those hours back to the zeroth date you get yourdate "floored" to the hour.






              share|improve this answer


























              • This does exactly what I need it to do, thank you!

                – Azhari
                Nov 26 '18 at 11:26
















              1














              Assuming the datatype is DATETIME or DATETIME2 you can use this common trick:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, yourdate), 0)


              DATEDIFF(HOUR, 0, yourdate) will give you the number of hours (and nothing else) between the zeroth date and yourdate. When you add those hours back to the zeroth date you get yourdate "floored" to the hour.






              share|improve this answer


























              • This does exactly what I need it to do, thank you!

                – Azhari
                Nov 26 '18 at 11:26














              1












              1








              1







              Assuming the datatype is DATETIME or DATETIME2 you can use this common trick:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, yourdate), 0)


              DATEDIFF(HOUR, 0, yourdate) will give you the number of hours (and nothing else) between the zeroth date and yourdate. When you add those hours back to the zeroth date you get yourdate "floored" to the hour.






              share|improve this answer















              Assuming the datatype is DATETIME or DATETIME2 you can use this common trick:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, yourdate), 0)


              DATEDIFF(HOUR, 0, yourdate) will give you the number of hours (and nothing else) between the zeroth date and yourdate. When you add those hours back to the zeroth date you get yourdate "floored" to the hour.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 11:41

























              answered Nov 26 '18 at 11:24









              Salman ASalman A

              185k67345441




              185k67345441













              • This does exactly what I need it to do, thank you!

                – Azhari
                Nov 26 '18 at 11:26



















              • This does exactly what I need it to do, thank you!

                – Azhari
                Nov 26 '18 at 11:26

















              This does exactly what I need it to do, thank you!

              – Azhari
              Nov 26 '18 at 11:26





              This does exactly what I need it to do, thank you!

              – Azhari
              Nov 26 '18 at 11:26













              3














              Another method is the DATEADD, DATEDIFF:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, '20000101',YourColumn),'20000101') AS RoundedValue
              FROM YourTable;





              share|improve this answer


























              • Can you explain what this is doing?

                – Azhari
                Nov 26 '18 at 11:22











              • @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

                – Larnu
                Nov 26 '18 at 11:23











              • I need it to round to the beginning of the current hour

                – Azhari
                Nov 26 '18 at 11:24











              • @Azhari and that's what it does.

                – Larnu
                Nov 26 '18 at 11:24






              • 1





                Small comment, there is a typo in the first date, one 0 missing.

                – SQL_M
                Nov 26 '18 at 11:28
















              3














              Another method is the DATEADD, DATEDIFF:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, '20000101',YourColumn),'20000101') AS RoundedValue
              FROM YourTable;





              share|improve this answer


























              • Can you explain what this is doing?

                – Azhari
                Nov 26 '18 at 11:22











              • @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

                – Larnu
                Nov 26 '18 at 11:23











              • I need it to round to the beginning of the current hour

                – Azhari
                Nov 26 '18 at 11:24











              • @Azhari and that's what it does.

                – Larnu
                Nov 26 '18 at 11:24






              • 1





                Small comment, there is a typo in the first date, one 0 missing.

                – SQL_M
                Nov 26 '18 at 11:28














              3












              3








              3







              Another method is the DATEADD, DATEDIFF:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, '20000101',YourColumn),'20000101') AS RoundedValue
              FROM YourTable;





              share|improve this answer















              Another method is the DATEADD, DATEDIFF:



              SELECT DATEADD(HOUR, DATEDIFF(HOUR, '20000101',YourColumn),'20000101') AS RoundedValue
              FROM YourTable;






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 11:29

























              answered Nov 26 '18 at 11:21









              LarnuLarnu

              22.4k51933




              22.4k51933













              • Can you explain what this is doing?

                – Azhari
                Nov 26 '18 at 11:22











              • @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

                – Larnu
                Nov 26 '18 at 11:23











              • I need it to round to the beginning of the current hour

                – Azhari
                Nov 26 '18 at 11:24











              • @Azhari and that's what it does.

                – Larnu
                Nov 26 '18 at 11:24






              • 1





                Small comment, there is a typo in the first date, one 0 missing.

                – SQL_M
                Nov 26 '18 at 11:28



















              • Can you explain what this is doing?

                – Azhari
                Nov 26 '18 at 11:22











              • @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

                – Larnu
                Nov 26 '18 at 11:23











              • I need it to round to the beginning of the current hour

                – Azhari
                Nov 26 '18 at 11:24











              • @Azhari and that's what it does.

                – Larnu
                Nov 26 '18 at 11:24






              • 1





                Small comment, there is a typo in the first date, one 0 missing.

                – SQL_M
                Nov 26 '18 at 11:28

















              Can you explain what this is doing?

              – Azhari
              Nov 26 '18 at 11:22





              Can you explain what this is doing?

              – Azhari
              Nov 26 '18 at 11:22













              @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

              – Larnu
              Nov 26 '18 at 11:23





              @Azhari It works out the difference in hours between '200-01-01' and your column, and then adds that many hours to '2000-01-01'.

              – Larnu
              Nov 26 '18 at 11:23













              I need it to round to the beginning of the current hour

              – Azhari
              Nov 26 '18 at 11:24





              I need it to round to the beginning of the current hour

              – Azhari
              Nov 26 '18 at 11:24













              @Azhari and that's what it does.

              – Larnu
              Nov 26 '18 at 11:24





              @Azhari and that's what it does.

              – Larnu
              Nov 26 '18 at 11:24




              1




              1





              Small comment, there is a typo in the first date, one 0 missing.

              – SQL_M
              Nov 26 '18 at 11:28





              Small comment, there is a typo in the first date, one 0 missing.

              – SQL_M
              Nov 26 '18 at 11:28











              1














              You can use DATETIMEFROMPARTS() Function.



              select DATETIMEFROMPARTS(YEAR(@date),MONTH(@date),DAY(@date),DATEPART(HOUR,@date),00,00,00)


              Here @date is replaced with YourColumn or any variable.






              share|improve this answer




























                1














                You can use DATETIMEFROMPARTS() Function.



                select DATETIMEFROMPARTS(YEAR(@date),MONTH(@date),DAY(@date),DATEPART(HOUR,@date),00,00,00)


                Here @date is replaced with YourColumn or any variable.






                share|improve this answer


























                  1












                  1








                  1







                  You can use DATETIMEFROMPARTS() Function.



                  select DATETIMEFROMPARTS(YEAR(@date),MONTH(@date),DAY(@date),DATEPART(HOUR,@date),00,00,00)


                  Here @date is replaced with YourColumn or any variable.






                  share|improve this answer













                  You can use DATETIMEFROMPARTS() Function.



                  select DATETIMEFROMPARTS(YEAR(@date),MONTH(@date),DAY(@date),DATEPART(HOUR,@date),00,00,00)


                  Here @date is replaced with YourColumn or any variable.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 11:31









                  Bhargav J PatelBhargav J Patel

                  1145




                  1145























                      0














                      This converts the value to a string, takes the left 14 chars and add the 00:00:00 values



                      select  left(convert(varchar , datecol, 121),14) + '00:00:00'  as mydatecol from table





                      share|improve this answer




























                        0














                        This converts the value to a string, takes the left 14 chars and add the 00:00:00 values



                        select  left(convert(varchar , datecol, 121),14) + '00:00:00'  as mydatecol from table





                        share|improve this answer


























                          0












                          0








                          0







                          This converts the value to a string, takes the left 14 chars and add the 00:00:00 values



                          select  left(convert(varchar , datecol, 121),14) + '00:00:00'  as mydatecol from table





                          share|improve this answer













                          This converts the value to a string, takes the left 14 chars and add the 00:00:00 values



                          select  left(convert(varchar , datecol, 121),14) + '00:00:00'  as mydatecol from table






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 11:36









                          WilhelmWilhelm

                          997




                          997






























                              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%2f53479857%2fround-datetime-to-the-beginning-of-the-current-hour%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

                              Wiesbaden

                              Marschland

                              Dieringhausen