Technical Article

FirstDayOfMonth() and LastDayOfMonth() functions

,

Here are a couple of handy date functions that pretty much describe themselves.  You can use these to figure out how many days are left in a month.

create function dbo.FirstDayOfMonth
(@TheDate as datetime)
returns datetime
as
/*****************************************
 *
 * copyright 2002 (c): don frazier
 * all rights reserved.
 *
 * Permission is granted to use this function
 * provided no fee is charge for its use
 * or distribution.
 * 
 * This notice must remain intact in all
 * executable and published copies.
 *
 * return first day of month of a given date
 *
 *****************************************/begin
  return dateadd(m, 0, dateadd(d,  ((datepart(d, @TheDate) - 1) * -1), @TheDate))
end


--

Create function dbo.LastDayOfMonth
(@TheDate as datetime)
returns datetime
as
/*****************************************
 *
 * copyright 2002 (c): don frazier
 * all rights reserved.
 *
 * Permission is granted to use this function
 * provided no fee is charge for its use
 * or distribution.
 * 
 * This notice must remain intact in all
 * executable and published copies.
 *
 * return last day of month of a given date
 *
 *****************************************/begin
  return dateadd(d, -1 , dateadd(m, 1, dateadd(d,  ((datepart(d, @TheDate) - 1) * -1), @TheDate)))
end

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating