set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: <zppro> -- Create date: <20060108> -- Description: <将当前时间转换为8位字符串'20060108'> -- ============================================= ALTERFUNCTION[dbo].[FormatDate2String] ( @SelectedDateasdatetime ) RETURNSint AS BEGIN -- Declare the return variable here DECLARE@Resultchar(12) DECLARE@Yearaschar(4) DECLARE@Monthaschar(2) DECLARE@Dayaschar(2) DECLARE@MaxNumasint DECLARE@Lengthasint DECLARE@PrefixAdditionasvarchar(2) set@Year=cast(year(@SelectedDate) aschar(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) asvarchar) 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) asvarchar) set@Result=@Year+@Month+@Day -- Return the result of the function RETURN@Result END