-- DECLARE @R AS DATETIME
-- EXECUTE dbo.FirstLastMonthDayDate @R=@R,@FL=2, @D='20100615'
-- EXECUTE dbo.FirstLastMonthDayDate @R=@R,@FL=1, @D='20100615'
CREATE PROCEDURE FirstLastMonthDayDate
@d AS DATETIME=NULL,
@FL AS INT=1,
@R AS DATETIME OUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF @d IS NULL
BEGIN
SELECT @d=GETDATE()
END
IF @FL=1
BEGIN
-- Return the first day date
SELECT @R=dateadd(day,-(datepart(day,@d)-1),@d)
END
ELSE
BEGIN
-- Return the last day date
SELECT @R=dateadd(day,-(datepart(day,dateadd(mm,1,@d))),dateadd(mm,1,@d))
END
SELECT @R
END
Niciun comentariu:
Trimiteți un comentariu