mysql varchar转年,MYSQL:如何将包含日期的VARCHAR列转换为DATE列?

I have a large table of birthdays that I want to convert from a varchar column to a date column.

The table SQL is:

CREATE TABLE `birthdays` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`uid` bigint(20) DEFAULT NULL,

`birthday_date` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM

The birthday date is stored in one of two ways: strings like "05/26/1994" or sometimes "03/14" (for people who don't want to reveal their birth year).

What is the best way to create another table and store the birthdays in a date column? Is it possible to do this just using MySQL (and avoid using PHP or some other intermediary)?

I have found a STR_TO_DATE function in MySQL. Should I use this?

Thanks!

解决方案SELECT IF(birthday_date RLIKE '[0-9]{2}/[0-9]{2}/[0-9]{4}',STR_TO_DATE(birthday_date,'%m/%d/%Y'),STR_TO_DATE(birthday_date,'%m/%d'));

This will result in dates like 0000-03-14 for rows that have no year entered. Your server needs to be configured to allow invalid dates though (see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值