create FUNCTION getday ( --声明要传入的参数 @time DATETIME ) RETURNS DATETIME AS BEGIN DECLARE @inday INT DECLARE @dtmDateNew DATETIME --取输入日期所在月份的第几天 SET @inday=DAY(@time) --获得这个月的第一天 SET @dtmDateNew=DATEADD(DAY,-@inday+1,@time) --获得这个月的最后一天 --SET @dtmDateNew=DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@time),120)+'1') RETURN @dtmDateNew end GO SELECT dbo.getday('2012-06-05') AS '当前时间'