Technical Article

getCalendarQTR

,

The script returns the quarter and year of a date submitted in the form of '105' - first quarter of 2005 - and is useful for grouping data by calendar quarter. Easy to modify for fiscal quarters.

CREATE FUNCTION [dbo].[getCalendarQTR](@Date as smalldatetime)
RETURNS char(3)
AS
BEGIN
DECLARE @QTR char(1)
DECLARE @YR char(2)
SET @YR = Year(@Date)
IF Month(@Date) IN (1,2,3)
SET @QTR = '1' 
IF Month(@Date) IN (4,5,6)
SET @QTR = '2' 
IF Month(@Date) IN (7,8,9)
SET @QTR = '3'
IF Month(@Date) IN (10,11,12)
SET @QTR = '4'
RETURN @QTR + @YR
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating