Create FUNCTION [dbo].[FormatDateTime] (@date as datetime,---传入时间 @dformat as varchar(10)---传入要生成的格式代码 ) RETURNS varchar(100) AS BEGIN declare @datestring as varchar(100) if @dformat='f1' begin set @datestring=(SELECT FORMAT(@date, 'yyyy-MM-dd HH:mm:ss.fff')) end if @dformat='f2' begin set @datestring=(SELECT FORMAT(@date, 'yyyy-MM-dd HH:mm:ss')); end if @dformat='f3' begin set @datestring=(SELECT FORMAT(@date, 'yyyyMMddHHmmss')); end if @dformat='f4' begin set @datestring=(SELECT FORMAT(@date, 'yyyy/MM/dd HH:mm:ss')); end if @dformat='f5' begin set @datestring=(SELECT FORMAT(@date, 'yyyy/MM/dd HH:mm:ss.fff')); end if @dformat='f6' begin set @datestring=(SELECT FORMAT(@date, 'yyyy/MM/dd HH:mm:ss:fff')); end if @dformat='f7' begin set @datestring=(SELECT FORMAT(@date, 'yyyy-MM-dd HH:mm:ss:fff')); end return @datestring END
标签:set,end,自定义,datestring,Server,dformat,SQL,date,SELECT From: https://www.cnblogs.com/dudian/p/18397825