mySQL中 實現Oracle 的to_char函數


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;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM