我正在尝试使用mysql中的str_to_date函数将TEXT字段转换为日期。现在正在运行:
mysql> select Date from sampleData limit 2;
+--------------+
| Date |
+--------------+
| "25-01-2012" |
| "25-01-2012" |
+--------------+
表明我们确实有文字。只是为了验证这里描述表的输出:
mysql> describe sampleData;
+--------------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+-------+
| Colors | tinyint(4) | YES | | NULL | |
| Date | text | NO | | NULL | |
+--------------------+------------+------+-----+---------+-------+
现在,当我尝试将其转换为日期时,有趣的部分:
mysql> select str_to_date(Date, '%d-%m-%Y') from sampleData limit 2;
+-------------------------------+
| str_to_date(Date, '%d-%m-%Y') |
+-------------------------------+
| NULL |
| NULL |
+-------------------------------+
哦,但是str_to_date只适用于字符类型而不是TEXT我认为所以我做了一个产生相同结果的强制转换。我做错了什么?
更新:回应评论
mysql> select AdId, str_to_date(Date, "%d-%m-%Y") from sampleData limit 2;
+----------+-------------------------------+
| AdId | str_to_date(Date, "%d-%m-%Y") |
+----------+-------------------------------+
| 84065013 | NULL |
| 84206047 | NULL |
+----------+-------------------------------+
和
mysql> select AdId, Date from sampleData limit 2;
+----------+--------------+
| AdId | Date |
+----------+--------------+
| 84065013 | "25-01-2012" |
| 84206047 | "25-01-2012" |
+----------+--------------+
表示正在处理的是相同的行(ID)。
答案 0 :(得分:3)
看起来你也有那些双引号。当您尝试使用双引号转换它时,您将获得null。
尝试做:
select str_to_date(REPLACE(Date,'"',''), '%d-%m-%Y')
from tab1 sampleData 2;