set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date, ,> -- Description: <Description, ,> -- ============================================= ALTERFUNCTION[dbo].[GetSerialNoByOrders] ( @NowDateasdatetime ) RETURNSchar(12) AS BEGIN -- Declare the return variable here DECLARE@Resultchar(12) DECLARE@Prefixaschar(8) DECLARE@Suffixaschar(4) DECLARE@MaxNumasint set@Prefix= dbo.FormatDate2String(@NowDate) -- Add the T-SQL statements to compute the return value here select@MaxNum=count(id) +1 from dbo.Orders wheredatediff(d,CheckinTime,@NowDate) =0 and Status <>0 if@MaxNum=0 begin set@Suffix='0001' end else begin DECLARE@Lengthasint DECLARE@PrefixAdditionasvarchar(4) set@Length=4-len( (@MaxNum) ) set@PrefixAddition='' while(@Length>0) begin set@PrefixAddition='0'+@PrefixAddition set@Length=@Length-1 end set@Suffix=@PrefixAddition+cast ( @MaxNumasvarchar) end set@Result=@Prefix+@Suffix -- Return the result of the function RETURN@Result END