Calculate last day in quarter CURRENT

What is the most efficient way to calculate the last day of the CURRENT quarter?

Example: given the date 3/5/09, I want to return 3/31/09.

ColdFusion Platform and SQL Server

+5
source share
4 answers
SELECT     DATEADD(qq, DATEDIFF(qq, - 1, '3/5/09'), - 1) 
+8
source

This answer uses the built-in functions of Quarter and DaysInMonth:

#createDate(year(now()), (quarter(now())*3), daysInMonth(createDate(year(now()), quarter(now())*3,1)) )#

It might be easier to read if you break it a little.


EDIT (@Sam Farmer: I took the liberty of turning your proposal into a CF function)

<cffunction name="LastOfQuarter" returntype="date" output="no" access="public">
  <cfargument name="d" type="date" required="no" default="#Now()#">

  <cfset d = CreateDate(Year(d), Quarter(d) * 3, 1)>
  <cfreturn DateAdd("d", d, DaysInMonth(d) - 1)>
</cffunction>
+4
source

- , .

<cffunction name="lastDayOfQuarter">
   <cfargument name="d" default="#now()#">
   <cfif month(d) lte 3>
     <cfreturn createDate(year(d),03,31)>
   </cfif>
   <cfif month(d) lte 6>
      <cfreturn createDate(year(d),06,30)>
   </cfif>
    <cfif month(d) lte 9>
      <cfreturn createDate(year(d),9,30)>
   </cfif>
   <cfreturn createDate(year(d),12,31)>
 </cffunction>
+3

:

SELECT dateadd(dd,-1,dateadd(qq,1,DATEADD(qq, DATEDIFF(qq,0,<date here>), 0)))

:

SELECT dateadd(dd,-1,dateadd(qq,1,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)))
0

All Articles