问题描述
表SQL是:
$ b我有一个很大的生日表,我想从一个varchar列转换为一个日期列。$ b
CREATE TABLE`birthdaydays`(
`id` bigint(20)NOT NULL AUTO_INCREMENT,
`uid` bigint(20) DEFAULT NULL,
`birthday_date` varchar(255)DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE = MyISAM
生日日期以两种方式之一存储:字符串,如05/26/1994或有时是03/14(适用于不想要的人)
创建另一个表格并将日历存储在日期列中的最佳方式是什么?是否可能这样做只是使用MySQL(并避免使用PHP或其他中介)?
我在MySQL中找到了一个STR_TO_DATE函数。我应该使用这个吗?
谢谢!
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'));
这将导致没有年份的行的日期如0000-03-14。您的服务器需要配置为允许无效日期(请参阅)
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 )
这篇关于MYSQL:如何将包含日期的VARCHAR列转换为DATE列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!