set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <zppro>
-- Create date: <20060108>
-- Description: <将当前时间转换为8位字符串'20060108'>
-- =============================================
ALTER FUNCTION [dbo].[FormatDate2String]
(
@SelectedDate as datetime
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @Result char(12)
DECLARE @Year as char(4)
DECLARE @Month as char(2)
DECLARE @Day as char(2)
DECLARE @MaxNum as int
DECLARE @Length as int
DECLARE @PrefixAddition as varchar(2)
set @Year = cast(year(@SelectedDate) as char(4))
set @Length = 2 - len(month(@SelectedDate))
set @PrefixAddition = ''
while(@Length > 0)
begin
set @PrefixAddition = '0' + @PrefixAddition
set @Length = @Length - 1
end
set @Month = @PrefixAddition + cast(month(@SelectedDate) as varchar)
set @Length = 2 - len(day(@SelectedDate))
set @PrefixAddition = ''
while(@Length > 0)
begin
set @PrefixAddition = '0' + @PrefixAddition
set @Length = @Length - 1
end
set @Day = @PrefixAddition + cast(day(@SelectedDate) as varchar)
set @Result = @Year + @Month + @Day
-- Return the result of the function
RETURN @Result
END

SQL日期格式化函数
1417

被折叠的 条评论
为什么被折叠?



