简体   繁体   English

SQL批量更改日期格式

[英]SQL CHANGE DATE FORMAT IN BULK

I have a database with several hundred fields but my data structure is wrong. 我有一个包含数百个字段的数据库,但是我的数据结构错误。 It is currently in uk format as follows: 当前采用英国格式,如下所示:

d/m /y
01/01/85
01/01/96
23/12/87

What would be the most efficient way to change the dates in bulk to sql standard of year/month/day 将批量日期更改为年/月/日的sql标准的最有效方法是什么

eg.
02/01/85 --> 1985/01/02
  1. Create a new DATE type column in your table: 在表中创建一个新的DATE类型列:

     ALTER TABLE myTable ADD newColumn DATE; 
  2. Use MySQL's STR_TO_DATE() function to parse the existing values into your new column: 使用MySQL的STR_TO_DATE()函数将现有值解析为新列:

     UPDATE myTable SET newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y'); 
  3. Change your codebase to use the new column. 更改您的代码库以使用新列。 If this can't happen immediately, you could define BEFORE INSERT and BEFORE UPDATE triggers to keep the new column consistent with the old one: 如果这不能立即发生,则可以定义BEFORE INSERTBEFORE UPDATE 触发器,以使新列与旧列保持一致:

     CREATE TRIGGER myTable_bi BEFORE INSERT ON myTable FOR EACH ROW SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y'); CREATE TRIGGER myTable_bu BEFORE UPDATE ON myTable FOR EACH ROW SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y'); 
  4. Drop the old column: 删除旧列:

     ALTER TABLE myTable DROP oldColumn; 

select date_format( date , '%Y-%m-%d') use this to change it to required format.I have used date_format function. 选择date_format( date ,'%Y-%m-%d')可以将其更改为所需的格式。我使用过date_format函数。 You can get more information about date_format here 您可以在此处获取有关date_format更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM