mysql date ad,尽管有效的格式化,MySQL str_to_date仍会生成NULL

I'm trying to use the str_to_date function from mysql to convert a TEXT field to a date. Now running:

mysql> select Date from sampleData limit 2;

+--------------+

| Date |

+--------------+

| "25-01-2012" |

| "25-01-2012" |

+--------------+

shows that we indeed have text. And just to verify here's the output from describe table:

mysql> describe sampleData;

+--------------------+------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------------------+------------+------+-----+---------+-------+

| Colors | tinyint(4) | YES | | NULL | |

| Date | text | NO | | NULL | |

+--------------------+------------+------+-----+---------+-------+

Now to the funny part when I try to convert this to a date:

mysql> select str_to_date(Date, '%d-%m-%Y') from sampleData limit 2;

+-------------------------------+

| str_to_date(Date, '%d-%m-%Y') |

+-------------------------------+

| NULL |

| NULL |

+-------------------------------+

Oh but str_to_date only works with character types and not TEXT I thought so I did a cast which produces the same result. What am I doing wrong?

UPDATE: In response to comment

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 |

+----------+-------------------------------+

and

mysql> select AdId, Date from sampleData limit 2;

+----------+--------------+

| AdId | Date |

+----------+--------------+

| 84065013 | "25-01-2012" |

| 84206047 | "25-01-2012" |

+----------+--------------+

which shows that it's the same lines (ID) that are being processed.

解决方案

It looks like you have those doublequotes in there also. When you try to convert it with the doublequotes, you get null.

Try doing:

select str_to_date(REPLACE(Date,'"',''), '%d-%m-%Y')

from tab1 sampleData 2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值