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. Why does MySQL not convert dates before 2010-01-01?

Why does MySQL not convert dates before 2010-01-01?

Scheduled Pinned Locked Moved Database
questionannouncementdatabasemysqlsales
4 Posts 3 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.
  • F Offline
    F Offline
    fd9750
    wrote on last edited by
    #1

    Hi, I have been trying to add some data to a table we get which is sometimes incomplete This is the SQL command that is used to fill in a "shipment_date"

    UPDATE IGNORE myTable
    SET Shipment_Date = CONCAT(LEFT(Sales_Order_Ship_Date,4), "-",
    MID(Sales_Order_Ship_Date, 2, 2), "-",
    RIGHT(Sales_Order_Ship_Date, 2) );

    In this case Shipment_date is an actual DATE data type whereas the Sales_Order_Ship_Date is a VARCHAR(42) data type. It works really well on the condition that the original string data represents a date of at least 2010-01-01. The string data is formatted as follows: YYYYMMDD so 2010-01-01 is 20100101 in the string version. It converts perfectly to 2010-01-01 and the same is true for later dates. Any string representing a date before 2010-01-01 though comes out as 0000-00-00. Does anyone have any ideas as to what is causing this?

    J Z 2 Replies Last reply
    0
    • F fd9750

      Hi, I have been trying to add some data to a table we get which is sometimes incomplete This is the SQL command that is used to fill in a "shipment_date"

      UPDATE IGNORE myTable
      SET Shipment_Date = CONCAT(LEFT(Sales_Order_Ship_Date,4), "-",
      MID(Sales_Order_Ship_Date, 2, 2), "-",
      RIGHT(Sales_Order_Ship_Date, 2) );

      In this case Shipment_date is an actual DATE data type whereas the Sales_Order_Ship_Date is a VARCHAR(42) data type. It works really well on the condition that the original string data represents a date of at least 2010-01-01. The string data is formatted as follows: YYYYMMDD so 2010-01-01 is 20100101 in the string version. It converts perfectly to 2010-01-01 and the same is true for later dates. Any string representing a date before 2010-01-01 though comes out as 0000-00-00. Does anyone have any ideas as to what is causing this?

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

      Well, MID('20091231',2,2) is '00' MID is used like: MID( string, start_position, length) where start position is one based, not zero based. So try using MID(Sales_Order_Ship_Date,5,2) instead.

      Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

      F 1 Reply Last reply
      0
      • J Jorgen Andersson

        Well, MID('20091231',2,2) is '00' MID is used like: MID( string, start_position, length) where start position is one based, not zero based. So try using MID(Sales_Order_Ship_Date,5,2) instead.

        Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

        F Offline
        F Offline
        fd9750
        wrote on last edited by
        #3

        Your answer was right on the mark, thanks a lot for that. :) I first tried it on MySQL Tryit Editor v1.0[^] and it worked perfectly there. Then I corrected my colleague's original code as you described and sure enough it now works exactly as expected. Why it ended up producing 0000-00-00 is still a mystery but once the dissection of the string is done properly it now accepts any date

        1 Reply Last reply
        0
        • F fd9750

          Hi, I have been trying to add some data to a table we get which is sometimes incomplete This is the SQL command that is used to fill in a "shipment_date"

          UPDATE IGNORE myTable
          SET Shipment_Date = CONCAT(LEFT(Sales_Order_Ship_Date,4), "-",
          MID(Sales_Order_Ship_Date, 2, 2), "-",
          RIGHT(Sales_Order_Ship_Date, 2) );

          In this case Shipment_date is an actual DATE data type whereas the Sales_Order_Ship_Date is a VARCHAR(42) data type. It works really well on the condition that the original string data represents a date of at least 2010-01-01. The string data is formatted as follows: YYYYMMDD so 2010-01-01 is 20100101 in the string version. It converts perfectly to 2010-01-01 and the same is true for later dates. Any string representing a date before 2010-01-01 though comes out as 0000-00-00. Does anyone have any ideas as to what is causing this?

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          Just use the CONVERT function,

          SELECT CONVERT(Sales_Order_Ship_Date , Date);

          Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

          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