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 convert(datetime, number) wrong?

SQL convert(datetime, number) wrong?

Scheduled Pinned Locked Moved Database
databasehelpquestion
13 Posts 5 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.
  • M Miller Nguyen

    I tried this

    select convert(datetime, 41818) as mydate

    result

    2014-06-30 00:00:00.000

    In Excel, it should be

    6/28/2014

    It's always 2 days in advance Please help! Thanks a lot

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

    Why do you think it's wrong? SELECT DATEDIFF ( dd , '1900-01-01' , '2014-06-30' ) SELECT DATEADD ( dd, 41818 , '1900-01-01' )

    Miller Nguyen wrote:

    In Excel, it should be

    What do you mean "it should be" ?

    You'll never get very far if all you do is follow instructions.

    M 1 Reply Last reply
    0
    • M Miller Nguyen

      I tried this

      select convert(datetime, 41818) as mydate

      result

      2014-06-30 00:00:00.000

      In Excel, it should be

      6/28/2014

      It's always 2 days in advance Please help! Thanks a lot

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #3

      What the f...? You are right, I tested it. Look at day number 0: with SQL Server, it is Jan 1, 1900; with Excel: Jan 0, 1900. The next bug is the leap year: Excel treats 1900 as a leap year (that's wrong!), while SQL Server correctly knows that 1900 is not a leap year. In sum, those differences account for the 2 days difference in current dates.

      J 1 Reply Last reply
      0
      • B Bernhard Hiller

        What the f...? You are right, I tested it. Look at day number 0: with SQL Server, it is Jan 1, 1900; with Excel: Jan 0, 1900. The next bug is the leap year: Excel treats 1900 as a leap year (that's wrong!), while SQL Server correctly knows that 1900 is not a leap year. In sum, those differences account for the 2 days difference in current dates.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #4

        Here's[^] Joels explanation.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        P 1 Reply Last reply
        0
        • M Miller Nguyen

          I tried this

          select convert(datetime, 41818) as mydate

          result

          2014-06-30 00:00:00.000

          In Excel, it should be

          6/28/2014

          It's always 2 days in advance Please help! Thanks a lot

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #5

          See my answer to Bernhard

          Wrong is evil and must be defeated. - Jeff Ello[^]

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            Why do you think it's wrong? SELECT DATEDIFF ( dd , '1900-01-01' , '2014-06-30' ) SELECT DATEADD ( dd, 41818 , '1900-01-01' )

            Miller Nguyen wrote:

            In Excel, it should be

            What do you mean "it should be" ?

            You'll never get very far if all you do is follow instructions.

            M Offline
            M Offline
            Miller Nguyen
            wrote on last edited by
            #6

            I don't say it's wrong, that's why I put the QUESTION MARK in the topic. I just wonder why Excel and SQL are giving different result

            1 Reply Last reply
            0
            • J Jorgen Andersson

              See my answer to Bernhard

              Wrong is evil and must be defeated. - Jeff Ello[^]

              M Offline
              M Offline
              Miller Nguyen
              wrote on last edited by
              #7

              Thanks a lot

              J 1 Reply Last reply
              0
              • M Miller Nguyen

                Thanks a lot

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #8

                Oh, btw, have a look if this[^] and this[^] would be of any use for you.

                Wrong is evil and must be defeated. - Jeff Ello[^]

                B 1 Reply Last reply
                0
                • J Jorgen Andersson

                  Oh, btw, have a look if this[^] and this[^] would be of any use for you.

                  Wrong is evil and must be defeated. - Jeff Ello[^]

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #9

                  For example, -0.5 and 0.5 both mean noon on 30 December 1899

                  No, no, no! I do not want to use that function!

                  J 1 Reply Last reply
                  0
                  • M Miller Nguyen

                    I tried this

                    select convert(datetime, 41818) as mydate

                    result

                    2014-06-30 00:00:00.000

                    In Excel, it should be

                    6/28/2014

                    It's always 2 days in advance Please help! Thanks a lot

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #10

                    I get around this, and other issues such as collation issues between SQL Server and Excel, by always passing dates to Excel as text in a format of dd-MMM-yyyy e.g. '01-Jan-2014'. It's not pretty but it has worked so far...

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    1 Reply Last reply
                    0
                    • B Bernhard Hiller

                      For example, -0.5 and 0.5 both mean noon on 30 December 1899

                      No, no, no! I do not want to use that function!

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #11

                      :laugh: It's not a bug, it's a feature.

                      Wrong is evil and must be defeated. - Jeff Ello[^]

                      1 Reply Last reply
                      0
                      • J Jorgen Andersson

                        Here's[^] Joels explanation.

                        Wrong is evil and must be defeated. - Jeff Ello[^]

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

                        Yeah, as I drifted off to sleep last night I recalled that Joel had written about Excel having to support a Lotus 123 issue. :thumbsup:

                        You'll never get very far if all you do is follow instructions.

                        J 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Yeah, as I drifted off to sleep last night I recalled that Joel had written about Excel having to support a Lotus 123 issue. :thumbsup:

                          You'll never get very far if all you do is follow instructions.

                          J Offline
                          J Offline
                          Jorgen Andersson
                          wrote on last edited by
                          #13

                          I love that story. They brought a fuck counter to all meetings with BillG. :laugh:

                          Wrong is evil and must be defeated. - Jeff Ello[^]

                          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