IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[CF_EOMonth]') 
                   and [type] = 'FN')
BEGIN
    DROP FUNCTION [dbo].[CF_EOMonth] 
END
GO

CREATE FUNCTION [dbo].[CF_EOMonth](@Fecha DATETIME) 
RETURNS DATETIME
AS
BEGIN
DECLARE @Retorno DATETIME
SET @Retorno = (DATEADD(mm,1,CAST(YEAR(@Fecha) AS VARCHAR(4)) + 
       RIGHT('00'+CAST(MONTH(@Fecha) AS VARCHAR(3)),2) + 
       '01') - 1)
RETURN @Retorno
END

GO


