English 中文(简体)
que/毫米/高
原标题:query sql convert text field dd/mm/yyyy to date field yyyy-mm-dd
  • 时间:2010-11-10 10:31:16
  •  标签:
  • mysql
  • date

I ve import a CSV file into mysql withdate form d/mm/yyyyyy.

我现在要问,如何将其从案文改为目前格式<代码>yy-mm-d。

最佳回答

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-Functions.html#Function_date-format”rel=“noretinger” STR_TO_DATE(str, Format) MySQL履行职能。

转换成一个:

BEGIN;
 ALTER TABLE `date_test` 
  ADD COLUMN `my_date_col_converted` DATE;

 UPDATE `date_test` 
  SET `my_date_col_converted` = STR_TO_DATE(`my_date_col`,  %d/%c/%Y );

 ALTER TABLE `date_test` 
  DROP COLUMN `my_date_col`;

 ALTER TABLE `date_test` 
  CHANGE COLUMN `my_date_col_converted` `my_date_col` DATE;
COMMIT;
问题回答

您可使用 DATE:

STR_TO_DATE( datefield , "%d/%m/%Y" )

If you need this DATE in a specific format, you can use DATE_FORMAT().
This probably isn t necessary in your case, but here s an example for completeness:

DATE_FORMAT( STR_TO_DATE( datefield , "%d/%m/%Y" ) , "%Y/%m/%d" )

因此,你可以在整个表格上用单一<代码>UPDATE将现有数据改为重新编号的数据(同时保持数据类型不变):

UPDATE tableName
SET originalDate = DATE_FORMAT(STR_TO_DATE(originalDate,"%d/%m/%Y" ),"%Y/%m/%d" );

Or, if you want to convert the datatype of the column DATE you could alter the table to create a new DATE formatted column, use the above update to fill that column, remove the original column, and then (optionally) rename the new column to the old name.

ALTER tableName
ADD modifiedDate DATE;

UPDATE tableName
SET modifiedDate = DATE_FORMAT( STR_TO_DATE( originalDate ,"%d/%m/%Y" ) ,"%Y/%m/%d" );

ALTER tableName
DROP COLUMN originalDate; 

ALTER tableName
CHANGE COLUMN modifiedDate originalDate;

这应当行之有效,但它并没有:

BEGIN;
 ALTER TABLE `date_test` 
  ADD COLUMN `my_date_col_converted` DATE;
 UPDATE `date_test` 
  SET `my_date_col_converted` = STR_TO_DATE(`my_date_col`,  %d/%c/%Y );
 ALTER TABLE `date_test` 
  DROP COLUMN `my_date_col`;
 ALTER TABLE `date_test` 
  CHANGE COLUMN `my_date_col_converted` `my_date_col` DATE;
COMMIT;

还应当努力: 工作

UPDATE db_test SET anticipated_court_date = DATE_FORMAT(STR_TO_DATE(anticipated_court_date,"%d/%m/%Y" ),"%Y-%m-%d" );

服务器版本:5.0.95-社区-log MySQL 社区版(GPL)

然而,这项工作:

SELECT STR_TO_DATE( 8/31/12 ,  %m/%d/%Y ); // WORKS

利用MySQL——我找不到任何可靠的解决办法。 即便是同一确切日期,也成功地转换了。

The solution I ve found is this:  PHP
$user_date = "8/31/12"; // WORKS
$mysql_date = date( Y-m-d H:i:s , strtotime($user_date));

以上是有助益的,但以下工作对我来说是关于我的。

ALTER TABLE `tablename` ADD `newcolumn` DATE NOT NULL ;
UPDATE `tablename` SET `newcolumn`=STR_TO_DATE(`oldcolumn`,  %d/%c/%Y )

现在删除旧栏(如果你愿意的话)。





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签