Get First and Last day of MonthOk, I know this is simple - but the snippet was in my archive so I'm going to post it. These two functions will return the first and/or last day of the month of the passed in datetime.
Example: SELECT dbo.GetFirstDayOfMonth(GETDATE())
Returns: 2013-01-02 00:00:00.000
Example: SELECT dbo.GetLastDayOfMonth(GETDATE())
Returns: 2013-02-28 23:59:59.990
CREATE FUNCTION GetFirstDayOfMonth
(
@DateTime DateTime
)
RETURNS DateTime
AS
BEGIN
RETURN Convert(DateTime, '1/' + Convert(VarChar, Month(@DateTime)) + '/' + Convert(VarChar, Year(@DateTime)) + ' 0:0:0.0')
END
CREATE FUNCTION GetLastDayOfMonth
(
@DateTime DateTime
)
RETURNS DateTime
AS
BEGIN
RETURN Convert(DateTime, Convert(VarChar, (DateAdd(MM, 1, @DateTime - Day(@DateTime) + 1) - 1), 101) + ' 23:59:59.99')
END
Is this code snippet, product or advice warrantied against ill-effect and/or technical malaise? No. No it's not! Not expressed - Not implied - not at all.