sql函数日期格式化为yyyy-mm-dd

道理很简单,如下:


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[trimDateTime] (@DATE datetime)
RETURNS varchar(10)
AS
BEGIN
RETURN cast(YEAR(@DATE) as char(4))+'-'+right('0'+cast(MONTH(@DATE) as varchar(2)),2)+'-'+right('0'+cast(DAY(@DATE) as varchar(2)),2)
END



--------------------------------------------

调用:


select dbo.trimDateTime('yyyy-m-d')

你可能感兴趣的:(SQL)