例子,创建sql自定义函数 Function dbo.fn_LastOfMonth(@TheDate DateTime)。
sql语句,如下:
6> Returns DateTime
7> AS
8> BEGIN
9> DECLARE @FirstOfMonth DateTime
10> DECLARE @DaysInMonth Int
11> DECLARE @RetDate DateTime
12> SET @FirstOfMonth = dateadd(mm, datediff(mm,0,@TheDate), 0)
13> SET @DaysInMonth = DATEDIFF(d, @FirstOfMonth, DATEADD(m, 1, @FirstOfMonth))
14> RETURN DATEADD(d, @DaysInMonth - 1, @FirstOfMonth)
15> END
16> GO
--删除自定义函数
2> drop function dbo.fn_LastOfMonth;
3> GO