Mega Code Archive

 
Categories / MSSQL Tutorial / Procedure Function
 

Creating a User-Defined Function

5>  CREATE Function dbo.fn_LastOfMonth(@TheDate DateTime) 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 1> 2> drop function dbo.fn_LastOfMonth; 3> GO 1>