I'm trying to convert varchar dates with multiple formats into a standard DateTime format so I can compare the dates between them. I wrote an SQL statement which I believe is on the right track but I know too little SQL to spot mistakes. 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.
It seems you forget to include an ENDIF
IF STR_TO_DATE ... THEN SET date = STR_TO_DATE( d, "%Y-%m-%d %H:%i:%s" );
ELSE
IF ...THEN SET date = STR_TO_DATE( d, "%H:%i:%s %b %d, %Y" );
END IF; /* close second if */
END IF; /* close first if */
see example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.