mySQL中 实现Oracle 的to_char函数,应该可以实现80%的兼容
REPLACE函数是区分大小的
DROP FUNCTION IF EXISTS `to_char`; CREATE DEFINER = `root`@`localhost` FUNCTION `to_char`(`fdate` datetime,`format` varchar(255)) RETURNS varchar(255) BEGIN DECLARE v_format VARCHAR(100); if fdate is null THEN RETURN ''; end if; if format is null THEN RETURN ''; end if; SET v_format=format; SET v_format = REPLACE(v_format,'hh24','%H'); SET v_format = REPLACE(v_format,'HH24','%H'); SET v_format = REPLACE(v_format,'hh','%H'); SET v_format = REPLACE(v_format,'HH','%H'); SET v_format = REPLACE(v_format,'mi','%i'); SET v_format = REPLACE(v_format,'MI','%i'); SET v_format = REPLACE(v_format,'mI','%i'); SET v_format = REPLACE(v_format,'Mi','%i'); SET v_format = REPLACE(v_format,'ss','%s'); SET v_format = REPLACE(v_format,'SS','%s'); SET v_format = REPLACE(v_format,'YYYY','%Y'); SET v_format = REPLACE(v_format,'yyyy','%Y'); SET v_format = REPLACE(v_format,'MM','%m'); SET v_format = REPLACE(v_format,'mm','%m'); SET v_format = REPLACE(v_format,'DD','%d'); SET v_format = REPLACE(v_format,'dd','%d'); RETURN DATE_FORMAT(fdate, v_format); END;