Wednesday, January 2, 2008

[mssql] Get First Date of Previous Month

CREATE FUNCTION dbo.udf_FirstDayofPreviousMonth (@pDate datetime)
RETURNS datetime
AS
BEGIN
DECLARE @dFirstDayPrefMonth AS datetime

SET @dFirstDayPrefMonth =
convert(datetime,
left(convert(varchar(8),
dateadd(MM,-1,
@pDate), 112), 6) + '01', 112)

RETURN @dFirstDayPrefMonth
END
GO

No comments:

Post a Comment