简体   繁体   English

我无法将 data_type 从 TINYTEXT 更改为 DATE&/DATETIME

[英]I can't change the data_type from TINYTEXT to DATE&/DATETIME

I'm using SQL 8.0 and still learning , and I did some good changes on it.我正在使用 SQL 8.0 并且仍在学习,我对它做了一些很好的改变。 Now i'm stuck by the date, they are in the data_type of TINYTEXT from the import and each of the other field are VARCHAR(60) or INT for the Primary Key and I did pretty good job on it but i'm stuck for the data_type of the date.现在我被日期困住了,它们在导入的TINYTEXTdata_type中,其他每个字段都是主键的VARCHAR(60)INT ,我在这方面做得很好,但我坚持该data_type日期。

I did this since the first day: it worked for date type我从第一天起就这样做了:它适用于date类型

SELECT *, DATE_FORMAT(column_name , "%d/%m/%Y %H:%i:%s")
FROM table_name; 

(For a Datetime It didn't work.) (对于日期时间它不起作用。)

now I tried this:现在我试过这个:

INSERT INTO database_test.table_test.field_name(today) 
VALUES(STR_TO_DATE('07-25-2012','%m-%d-%y') ) 

Instead of %m-%d%y , I need this European format: %d/%m/%Y for DATE.而不是%m-%d%y ,我需要这种欧洲格式: %d/%m/%Y用于 DATE。 And %d/%m/%Y %H:%i:%s for my DATETIME .%d/%m/%Y %H:%i:%s为我的DATETIME Both are in TINYTEXT .两者都在TINYTEXT (NB:they are date from 1899 to 2099 for the future "DATE" data_type.) (注意:对于未来的“DATE”数据类型,它们的日期是从 1899 年到 2099 年。)

I'm going to make a couple of assumptions...我要做几个假设……

  1. by SQL 8.0 you mean MySQL 8.0 ( STR_TO_DATE is non-standard so may not work with other database servers) SQL 8.0是指MySQL 8.0STR_TO_DATE是非标准的,因此可能不适用于其他数据库服务器)
  2. dates in your TINYTEXT field are stored in the format %d/%m/%Y %H:%i:%s TINYTEXT字段中的日期以%d/%m/%Y %H:%i:%s格式存储

... and for the sake of argument I'm going to say the TINYTEXT date field is called date_string . ...为了论证,我将说TINYTEXT日期字段称为date_string

What I'd do is create a new DATE column and then UPDATE it with the value from date_string - using the STR_TO_DATE function (like you tried) - and if that's successful remove the original column and, possibly, rename the new DATE column.我会做的是创建一个新的DATE列,然后UPDATE从价值它date_string -使用STR_TO_DATE功能(如你试过) -如果这就是成功删除原始列,并且可能重命名新DATE列。


Add the real date column添加真实日期

I'm calling it date_date here.我在这里称它为date_date

ALTER TABLE `table_test` ADD `date_date` DATE NOT NULL AFTER `date_string`;


UPDATE the real date with the value from the TINYTEXT field使用 TINYTEXT 字段中的值更新实际日期

(assuming d/m/YH:i:s format) (假设d/m/YH:i:s格式)

UPDATE `table_test` SET `date_date` = STR_TO_DATE(`date_string`, '%d/%m/%Y %H:%i:%s');


That should work as long as the format in your TINYTEXT column matches the format set in the STR_TO_DATE function.只要您的TINYTEXT列中的格式与STR_TO_DATE函数中设置的格式相匹配,这应该有效。 You'll have the original and converted dates in the table for now anyway, so you can check that the data looks OK.无论如何,您现在将在表中拥有原始日期和转换日期,因此您可以检查数据是否正常。

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

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