How can i get the hour between two datetime in sql?












0















Below sql showing 57 hours, But,it's 44 hrs. How can i solve.



SELECT  DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')









share|improve this question





























    0















    Below sql showing 57 hours, But,it's 44 hrs. How can i solve.



    SELECT  DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')









    share|improve this question



























      0












      0








      0








      Below sql showing 57 hours, But,it's 44 hrs. How can i solve.



      SELECT  DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')









      share|improve this question
















      Below sql showing 57 hours, But,it's 44 hrs. How can i solve.



      SELECT  DATEDIFF(Hour,'2018-11-20 2:26:38.000','2018-11-22 11:00:29.367')






      sql-server sql-server-2008 select hour






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 10:55







      Shahana Akter Pingkey

















      asked Nov 22 '18 at 10:51









      Shahana Akter PingkeyShahana Akter Pingkey

      165




      165
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Use 24 hr format in both dates



          SELECT  DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')





          share|improve this answer
























          • How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

            – Shahana Akter Pingkey
            Nov 22 '18 at 10:56











          • you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

            – Anubrij Chandra
            Nov 22 '18 at 11:00








          • 1





            @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

            – Larnu
            Nov 22 '18 at 11:01













          • Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

            – Shahana Akter Pingkey
            Nov 22 '18 at 11:04






          • 1





            @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

            – Larnu
            Nov 22 '18 at 11:05



















          0














          Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.




          /*------------------------
          SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
          ------------------------*/

          45


          You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.



          Using two digits for the hour should help.




          /*------------------------
          SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
          ------------------------*/

          57


          (SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)



          If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.






          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%2f53429301%2fhow-can-i-get-the-hour-between-two-datetime-in-sql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Use 24 hr format in both dates



            SELECT  DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')





            share|improve this answer
























            • How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

              – Shahana Akter Pingkey
              Nov 22 '18 at 10:56











            • you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

              – Anubrij Chandra
              Nov 22 '18 at 11:00








            • 1





              @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

              – Larnu
              Nov 22 '18 at 11:01













            • Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

              – Shahana Akter Pingkey
              Nov 22 '18 at 11:04






            • 1





              @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

              – Larnu
              Nov 22 '18 at 11:05
















            2














            Use 24 hr format in both dates



            SELECT  DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')





            share|improve this answer
























            • How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

              – Shahana Akter Pingkey
              Nov 22 '18 at 10:56











            • you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

              – Anubrij Chandra
              Nov 22 '18 at 11:00








            • 1





              @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

              – Larnu
              Nov 22 '18 at 11:01













            • Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

              – Shahana Akter Pingkey
              Nov 22 '18 at 11:04






            • 1





              @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

              – Larnu
              Nov 22 '18 at 11:05














            2












            2








            2







            Use 24 hr format in both dates



            SELECT  DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')





            share|improve this answer













            Use 24 hr format in both dates



            SELECT  DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 10:54









            Anubrij ChandraAnubrij Chandra

            7311719




            7311719













            • How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

              – Shahana Akter Pingkey
              Nov 22 '18 at 10:56











            • you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

              – Anubrij Chandra
              Nov 22 '18 at 11:00








            • 1





              @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

              – Larnu
              Nov 22 '18 at 11:01













            • Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

              – Shahana Akter Pingkey
              Nov 22 '18 at 11:04






            • 1





              @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

              – Larnu
              Nov 22 '18 at 11:05



















            • How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

              – Shahana Akter Pingkey
              Nov 22 '18 at 10:56











            • you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

              – Anubrij Chandra
              Nov 22 '18 at 11:00








            • 1





              @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

              – Larnu
              Nov 22 '18 at 11:01













            • Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

              – Shahana Akter Pingkey
              Nov 22 '18 at 11:04






            • 1





              @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

              – Larnu
              Nov 22 '18 at 11:05

















            How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

            – Shahana Akter Pingkey
            Nov 22 '18 at 10:56





            How can i convert Datetime into 24 hrs format '2018-11-20 2:26:38.000'

            – Shahana Akter Pingkey
            Nov 22 '18 at 10:56













            you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

            – Anubrij Chandra
            Nov 22 '18 at 11:00







            you have include AM or PM to covert it into 24 Hr format SELECT CONVERT(DATETIME, '2018-11-20 2:00:00 PM', 0)

            – Anubrij Chandra
            Nov 22 '18 at 11:00






            1




            1





            @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

            – Larnu
            Nov 22 '18 at 11:01







            @ShahanaAkterPingkey if you've not stored whether the time is AM or PM, then then you've already lost that information. If I said the time is 11:01 and asked you what time that was in 24 hour format, without additional information, how do you know it it's 11:01 or 23:01? (Answer: you don't). SQL Server can't infer this information either.

            – Larnu
            Nov 22 '18 at 11:01















            Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

            – Shahana Akter Pingkey
            Nov 22 '18 at 11:04





            Ist date i am storing it can be AM/PM . But , last date i compared with the GETDATE()

            – Shahana Akter Pingkey
            Nov 22 '18 at 11:04




            1




            1





            @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

            – Larnu
            Nov 22 '18 at 11:05





            @ShahanaAkterPingkey so, how do you tell if it's AM/PM?

            – Larnu
            Nov 22 '18 at 11:05













            0














            Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.




            /*------------------------
            SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
            ------------------------*/

            45


            You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.



            Using two digits for the hour should help.




            /*------------------------
            SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
            ------------------------*/

            57


            (SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)



            If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.






            share|improve this answer




























              0














              Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.




              /*------------------------
              SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
              ------------------------*/

              45


              You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.



              Using two digits for the hour should help.




              /*------------------------
              SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
              ------------------------*/

              57


              (SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)



              If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.






              share|improve this answer


























                0












                0








                0







                Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.




                /*------------------------
                SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
                ------------------------*/

                45


                You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.



                Using two digits for the hour should help.




                /*------------------------
                SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
                ------------------------*/

                57


                (SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)



                If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.






                share|improve this answer













                Not prefixing the hours to be two digits looks like it could be the issue, making it unambiguously in the afternoon gives your result.




                /*------------------------
                SELECT DATEDIFF(Hour,'2018-11-20 14:26:38.000','2018-11-22 11:00:29.367')
                ------------------------*/

                45


                You're default Date Time format is probably 12 hour, and "2:…" is being treated as pm.



                Using two digits for the hour should help.




                /*------------------------
                SELECT DATEDIFF(Hour,'2018-11-20 02:26:38.000','2018-11-22 11:00:29.367')
                ------------------------*/

                57


                (SQL Server has a lot of backward compatibility which may be triggered is the input is not precisely formatted, ISO Date/Time formats always use two digits for hours, minutes, and seconds.)



                If you are using a client (rather than fixed code in a Stored Proc/Function/Trigger/…) parameterising your queries avoids this issue: pass data as Date-Time type directly without any need to convert into a string.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 11:02









                RichardRichard

                89.5k17155221




                89.5k17155221






























                    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%2f53429301%2fhow-can-i-get-the-hour-between-two-datetime-in-sql%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

                    'app-layout' is not a known element: how to share Component with different Modules

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    WPF add header to Image with URL pettitions [duplicate]