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


CREATE FUNCTION [dbo].[CF_FOMONTH](@Fecha DATETIME) 
RETURNS DATETIME
AS
BEGIN

/*Devuelve el primer dia del mes*/

DECLARE @Retorno DATETIME
SET @Retorno = CAST(   CAST(YEAR(@Fecha) AS VARCHAR(4)) +  RIGHT('00'+CAST(MONTH(@Fecha) AS VARCHAR(3)),2) + '01' AS DATETIME)
RETURN @Retorno
END
