SQL function to return the start of Quarter by supplying a date

SQL function to return the start of Quarter by supplying a date. For example, if you supply April 4, 2008, this function will return April1, 2008 which is the start of second quarter.


CREATE FUNCTION RETURN_START_OF_QUARTER_FUN
(
@P_IN_DATE DATETIME
)
RETURNS DATETIME

AS

BEGIN

DECLARE @CURRENT_MONTH INT
DECLARE @CURRENT_YEAR INT
DECLARE @START_OF_CURRENT_QUARTER DATETIME

SET @CURRENT_MONTH = DATEPART(MONTH,@P_IN_DATE)
SET @CURRENT_YEAR = DATEPART(YEAR,@P_IN_DATE)

IF @CURRENT_MONTH <= 3
SET @START_OF_CURRENT_QUARTER = DATEADD(DAY,0,'1/1/' + CONVERT(VARCHAR(4),@CURRENT_YEAR))
ELSE IF @CURRENT_MONTH BETWEEN 4 AND 6
SET @START_OF_CURRENT_QUARTER = DATEADD(DAY,0,'4/1/' + CONVERT(VARCHAR(4),@CURRENT_YEAR))
ELSE IF @CURRENT_MONTH BETWEEN 7 AND 9
SET @START_OF_CURRENT_QUARTER = DATEADD(DAY,0,'7/1/' + CONVERT(VARCHAR(4),@CURRENT_YEAR))
ELSE
SET @START_OF_CURRENT_QUARTER = DATEADD(DAY,0,'10/1/' + CONVERT(VARCHAR(4),@CURRENT_YEAR))

RETURN @START_OF_CURRENT_QUARTER

END


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: