[英]query sql convert text field dd/mm/yyyy to date field yyyy-mm-dd
I've imported a CSV file into mysql with dates in format dd/mm/yyyy
.我已经将一个 CSV 文件导入到 mysql 中,日期格式为
dd/mm/yyyy
。
I now need a query to convert it from text to date format yyy-mm-dd
.我现在需要一个查询来将它从文本转换为日期格式
yyy-mm-dd
。
You could use the STR_TO_DATE(str, format) MySQL function.您可以使用STR_TO_DATE(str, format) MySQL 函数。
Example switching out my_date_col
for a converted one:将
my_date_col
切换为转换后的示例:
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;
You can use STR_TO_DATE()
in the following way to convert your text in to a DATE
:您可以通过以下方式使用
STR_TO_DATE()
将文本转换为DATE
:
STR_TO_DATE( datefield , "%d/%m/%Y" )
If you need this DATE
in a specific format, you can use DATE_FORMAT()
.如果您需要特定格式的
DATE
,您可以使用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" )
So, you could do this over the whole table with a single UPDATE
to replace the current data with the reformatted data (while keeping the datatype the same):因此,您可以使用单个
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.或者,如果您想转换列
DATE
的数据类型,您可以更改表以创建新的DATE
格式列,使用上面的更新来填充该列,删除原始列,然后(可选)将新列重命名为旧名。
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;
This should work but it doesn't:这应该有效,但无效:
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;
Also this should work: Doesn't Work这也应该有效:不起作用
UPDATE db_test SET anticipated_court_date = DATE_FORMAT(STR_TO_DATE(anticipated_court_date,"%d/%m/%Y" ),"%Y-%m-%d" );
Server version: 5.0.95-community-log MySQL Community Edition (GPL)服务器版本:5.0.95-community-log MySQL Community Edition (GPL)
However this works:但是这有效:
SELECT STR_TO_DATE('8/31/12', '%m/%d/%Y'); // WORKS
Using MySQL - I couldn't find any solution that worked reliably.使用 MySQL - 我找不到任何可靠的解决方案。 Even the same exact date wasn't converted successfully.
即使是相同的确切日期也没有成功转换。
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));
The above were helpful, but the following worked for me on Mysql.以上内容很有帮助,但以下内容在 Mysql 上对我有用。
ALTER TABLE `tablename` ADD `newcolumn` DATE NOT NULL ;
UPDATE `tablename` SET `newcolumn`=STR_TO_DATE(`oldcolumn`, '%d/%c/%Y')
Now delete the oldcolumn (if you wish).现在删除旧列(如果您愿意)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.