[英]MySQL:Convert multiple varchar formats into DateTime
I'm trying to convert varchar dates with multiple formats into a standard DateTime format so I can compare the dates between them. 我正在尝试将具有多种格式的varchar日期转换为标准DateTime格式,以便可以比较它们之间的日期。 I wrote an SQL statement which I believe is on the right track but I know too little SQL to spot mistakes.
我写了一条SQL语句,我相信它是正确的,但是我知道很少有SQL可以发现错误。 My current code errors out.
我当前的代码错误出来了。 Here's what I have:
这是我所拥有的:
DELIMITER //
CREATE FUNCTION CUSTOM_DATE_CONVERT ( d VARCHAR(50) )
RETURNS DATETIME
BEGIN
DECLARE date DATETIME;
IF STR_TO_DATE
( d, "%Y-%m-%d %H:%i:%s" ) IS NOT NULL THEN SET date = STR_TO_DATE( d, "%Y-%m-%d %H:%i:%s" );
ELSE
IF STR_TO_DATE( d, "%H:%i:%s %b %d, %Y" ) IS NOT NULL THEN SET date = STR_TO_DATE( d, "%H:%i:%s %b %d, %Y" );
END IF;
RETURN date;
END//
DELIMITER ;
SELECT id, CUSTOM_DATE_CONVERT( payment_date ) FROM `paypal_table`
And my error is: 我的错误是:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 12
Am I on the right track? 我在正确的轨道上吗? Any help is greatly appreciated.
任何帮助是极大的赞赏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.