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
