6

I am looking for a way to change the datatype of a column. Currently, in my database, the date columns types were defined as varchar and I need to convert them back to the date type.

Any idea how to do it?

baduker
  • 19,152
  • 9
  • 33
  • 56
Mujahid
  • 1,227
  • 5
  • 32
  • 62

6 Answers6

5

You will need to adapt this based your your exact table structure but something like;

CREATE TABLE temp (startdate varchar(255), stuff varchar(255));

INSERT INTO temp
SELECT startdate,stuff
FROM mytable;

TRUNCATE TABLE mytable;

ALTER TABLE mytable ALTER COLUMN startdate DATETIME NOT NULL;

INSERT INTO mytable
SELECT CAST(startdate AS DATETIME), stuff FROM temp;

DROP TABLE temp;
Tom Squires
  • 8,848
  • 12
  • 46
  • 72
  • I did it, but at the end, the datetime column values changed to NULL :( – Mujahid Aug 17 '11 at 09:21
  • the current date format in varchar column is : 4/9/2011 (m/d/y) – Mujahid Aug 17 '11 at 09:22
  • Does SELECT CAST(startdate AS DATETIME), stuff FROM temp; return null for that column too? – Tom Squires Aug 17 '11 at 09:25
  • 1
    yes, I think we should use STR_TO_DATE(startdate,format) right? – Mujahid Aug 17 '11 at 09:28
  • 1
    @Tome: Thanks a lot mate, now its working. following is the code `INSERT INTO mytable SELECT id, STR_TO_DATE(startdate,'%m/%d/%Y'),STR_TO_DATE(enddate,'%m/%d/%Y'), tname,handicap, club, url FROM temp;` If you haven't provide the above code structure, I wont be able to do it. Thanks a lot :) – Mujahid Aug 17 '11 at 09:36
  • HI Tom, please let me know one more thing, how can I update only 1 column data taking from another table as from the above method – Mujahid Aug 24 '11 at 05:45
  • @Mujahid I dont understand what your asking. Can you open a new question going into detail what the problem is? – Tom Squires Aug 24 '11 at 14:22
2

First, create the new column with type data Next, run update query, to populate the new column with the value of the old one, applying any conversion if needed Next, drop the old column Finally, rename the new column to the old one

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1
  1. Create a new DATE column with a temporary name
  2. Populate the new column with an UPDATE query that makes use of STR_TO_DATE()
  3. If everything's right, remove the VARCHAR column and rename the DATE column.
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

Mysql default date format is : YYYY-MM-DD . If your try to insert the date otherwise, as you actually did, the date will be inserted with these values : 000-00-00, giving you a hint to the acceptable date format for mySql.

0

Wanna share this for SQL server users. For me this method is much convenient and safer.

  1. In your Table create new column "NewDate" (temporarily or name whatever you want).
  2. Make sure no invalid Datetime format in the Table you want to convert. Try those formats here: https://www.w3schools.com/sql/trysqlserver.asp?filename=trysql_func_sqlserver_cast3 <--- you need to check thoroughly otherwise there would be an error executing the command below.

  3. Execute this command:

UPDATE myTable SET NewDate = CAST(OldDate AS datetime) WHERE (OldDate <> '') AND (OldDate IS NOT NULL) --to make sure you cast only what is needed otherwise there would be an error.

  1. You can now delete the old column i.e. "OldDate".
  2. Finally you can drag and drop the new table you've created to the slot where you just deleted the old column in the table design.
0

If the field of your column is VARCHAR and stored date as DD-MM-YYYY then we have to convert the date in YYYY-MM-DD format by following PHP code.

$cd = array();

$cd1 = array();

$cdf = array();

$getdata = mysqli_query($link,"SELECT columnname FROM tablename");

while($row=mysqli_fetch_array($getdata))        
{   
    $cd = $row['columnname'];

    $cd1 = strtotime($cd);

    $cdf = date('Y-m-d',$cd1);

    mysqli_query($link,"UPDATE tablename SET columnname = 
    REPLACE(columnname,'$cd','$cdf')");
}

After running this PHP code, in your MySQL table change the datatype of your column to 'DATE'. It works for me without losing or truncate data.

Boken
  • 4,825
  • 10
  • 32
  • 42