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 Server null date 01/01/1900 possible issue - a question

SQL Server null date 01/01/1900 possible issue - a question

Scheduled Pinned Locked Moved Database
databasehelpquestionsql-serversysadmin
9 Posts 5 Posters 11 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.
  • G Offline
    G Offline
    GuyThiebaut
    wrote on last edited by
    #1

    SQL Server will return a null date as 01/01/1900 if one castes a null string to a date. Now the issue is that someone had the idea of populating dates with 01/01/1900 to signify a certain stage with a row(meaning that the date is not known but it is known that that stage has been reached). I only realised yesterday that this could cause an issue if a query is written where a null string is compared to one of these dates. Does anyone else have experience of this and any suggestions? I am currently thinking of changing these 01/01/1900 dates to 01/01/1901 to remedy this potential problem. Thanks

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

    ― Christopher Hitchens

    Richard DeemingR L J 3 Replies Last reply
    0
    • G GuyThiebaut

      SQL Server will return a null date as 01/01/1900 if one castes a null string to a date. Now the issue is that someone had the idea of populating dates with 01/01/1900 to signify a certain stage with a row(meaning that the date is not known but it is known that that stage has been reached). I only realised yesterday that this could cause an issue if a query is written where a null string is compared to one of these dates. Does anyone else have experience of this and any suggestions? I am currently thinking of changing these 01/01/1900 dates to 01/01/1901 to remedy this potential problem. Thanks

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

      ― Christopher Hitchens

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Which version of SQL Server? I've just tried with SQL 2008, and casting a varchar containing Null to either datetime, smalldatetime, datetime2(0) or date returns Null in all cases. http://www.sqlfiddle.com/#!3/d41d8/13023[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • G GuyThiebaut

        SQL Server will return a null date as 01/01/1900 if one castes a null string to a date. Now the issue is that someone had the idea of populating dates with 01/01/1900 to signify a certain stage with a row(meaning that the date is not known but it is known that that stage has been reached). I only realised yesterday that this could cause an issue if a query is written where a null string is compared to one of these dates. Does anyone else have experience of this and any suggestions? I am currently thinking of changing these 01/01/1900 dates to 01/01/1901 to remedy this potential problem. Thanks

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

        ― Christopher Hitchens

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I'd suggest adding a bit-column called "HasDate".

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • G GuyThiebaut

          SQL Server will return a null date as 01/01/1900 if one castes a null string to a date. Now the issue is that someone had the idea of populating dates with 01/01/1900 to signify a certain stage with a row(meaning that the date is not known but it is known that that stage has been reached). I only realised yesterday that this could cause an issue if a query is written where a null string is compared to one of these dates. Does anyone else have experience of this and any suggestions? I am currently thinking of changing these 01/01/1900 dates to 01/01/1901 to remedy this potential problem. Thanks

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

          ― Christopher Hitchens

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          GuyThiebaut wrote:

          SQL Server will return a null date as 01/01/1900 if one castes a null string to a date.

          I seriously doubt that. As a guess the data is actually an empty string.

          Richard DeemingR G 2 Replies Last reply
          0
          • J jschell

            GuyThiebaut wrote:

            SQL Server will return a null date as 01/01/1900 if one castes a null string to a date.

            I seriously doubt that. As a guess the data is actually an empty string.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            jschell wrote:

            the data is actually an empty string

            Yes, that makes sense: http://www.sqlfiddle.com/#!3/d41d8/13027[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • J jschell

              GuyThiebaut wrote:

              SQL Server will return a null date as 01/01/1900 if one castes a null string to a date.

              I seriously doubt that. As a guess the data is actually an empty string.

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

              Thanks. I will have to check this with regards to the data I have been looking at - however I think you have hit the proverbial nail on the head. I think I will go with changing the dates to something other than 01/01/1900 as I can see someone in the future doing an isnull comparison on an empty string and getting a match against these dates.

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

              ― Christopher Hitchens

              J 1 Reply Last reply
              0
              • G GuyThiebaut

                Thanks. I will have to check this with regards to the data I have been looking at - however I think you have hit the proverbial nail on the head. I think I will go with changing the dates to something other than 01/01/1900 as I can see someone in the future doing an isnull comparison on an empty string and getting a match against these dates.

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

                ― Christopher Hitchens

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                GuyThiebaut wrote:

                as I can see someone in the future doing an isnull comparison on an empty string and getting a match against these dates.

                I don't understand that statement so just to be sure I will state that an empty string is not the same as a null value.

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Which version of SQL Server? I've just tried with SQL 2008, and casting a varchar containing Null to either datetime, smalldatetime, datetime2(0) or date returns Null in all cases. http://www.sqlfiddle.com/#!3/d41d8/13023[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  A Offline
                  A Offline
                  Aner Bautista
                  wrote on last edited by
                  #8

                  Hello, this is not SQL Server Issue. This is related to the Connection Type you ar using (ADO.NET, OLE DB, ODBC...). for example: If you are using MS Access you will see: [Null] dates returns/saves [01/01/1900] [Bit] field returns [-1] or [1] Just a moment ago a friend asked me about the same problem, he is developing on Visual Studio .NET 2008 SP1 (I really did not checked his code, but he resolved his problem). Hope this help. Best regards.

                  J 1 Reply Last reply
                  0
                  • A Aner Bautista

                    Hello, this is not SQL Server Issue. This is related to the Connection Type you ar using (ADO.NET, OLE DB, ODBC...). for example: If you are using MS Access you will see: [Null] dates returns/saves [01/01/1900] [Bit] field returns [-1] or [1] Just a moment ago a friend asked me about the same problem, he is developing on Visual Studio .NET 2008 SP1 (I really did not checked his code, but he resolved his problem). Hope this help. Best regards.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    MS Access might do that but I seriously doubt SQL Server does. If it is null in the database then a null is returned to .Net. No manner of casting/manipulation at that point in time is going to change that to something that isn't a null.

                    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