SQL 存储过程 日期格式化
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER Function [dbo].[GetFormattedDate]( @Date As datetime)
Returns varchar(20)
As
Begin
Declare @formattedDate as varchar(20)
Declare @Year as varchar(20)
Declare @Month as varchar(20)
Declare @Day as varchar(20)
SET @Year = DATENAME(Year,@Date)
SET @Month = Cast(DATEPart(Month,@Date) As Varchar(10))
IF Len(@Month) = 1
SET @Month = '0' + @Month
SET @Day = DATENAME(Day,@Date)
IF Len(@Day) = 1
SET @Day = '0' + @Day
SET @formattedDate = @Year + '-' + @Month + '-' + @Day
Return @formattedDate
End
编辑器加载中...